function cancelAgreement(uint256 agreementId) senderOnly(agreementId) external {
        Agreement storage record = agreements[agreementId];

        require(!record.cancelled);

        if (withdrawAmount(agreementId) > 0) {
            withdrawTokens(agreementId);
        }
        
        uint256 releasedAmount = record.releasedAmount;
        uint256 cancelledAmount = record.totalAmount.sub(releasedAmount); 
        
        record.token.transfer(record.sender, cancelledAmount);
        record.cancelled = true;
        
        emit AgreementCancelled(
            agreementId,
            address(record.token),
            record.recipient,
            record.sender,
            record.start,
            releasedAmount,
            cancelledAmount,
            block.timestamp
        );
    }