```\nFile: VaultAccount.sol\n    function repayAccountPrimeDebtAtSettlement(\n        PrimeRate memory pr,\n        VaultStateStorage storage primeVaultState,\n        uint16 currencyId,\n        address vault,\n        address account,\n        int256 accountPrimeCash,\n        int256 accountPrimeStorageValue\n    ) internal returns (int256 finalPrimeDebtStorageValue, bool didTransfer) {\n        didTransfer = false;\n        finalPrimeDebtStorageValue = accountPrimeStorageValue;\n        \n        if (accountPrimeCash > 0) {\n            // netPrimeDebtRepaid is a negative number\n            int256 netPrimeDebtRepaid = pr.convertUnderlyingToDebtStorage(\n                pr.convertToUnderlying(accountPrimeCash).neg()\n            );\n\n            int256 netPrimeDebtChange;\n            if (netPrimeDebtRepaid < accountPrimeStorageValue) {\n                // If the net debt change is greater than the debt held by the account, then only\n                // decrease the total prime debt by what is held by the account. The residual amount\n                // will be refunded to the account via a direct transfer.\n                netPrimeDebtChange = accountPrimeStorageValue;\n                finalPrimeDebtStorageValue = 0;\n\n                int256 primeCashRefund = pr.convertFromUnderlying(\n                    pr.convertDebtStorageToUnderlying(netPrimeDebtChange.sub(accountPrimeStorageValue))\n                );\n                TokenHandler.withdrawPrimeCash(\n                    account, currencyId, primeCashRefund, pr, false // ETH will be transferred natively\n                );\n                didTransfer = true;\n            } else {\n                // In this case, part of the account's debt is repaid.\n                netPrimeDebtChange = netPrimeDebtRepaid;\n                finalPrimeDebtStorageValue = accountPrimeStorageValue.sub(netPrimeDebtRepaid);\n            }\n\n            // Updates the global prime debt figure and events are emitted via the vault.\n            pr.updateTotalPrimeDebt(vault, currencyId, netPrimeDebtChange);\n\n            // Updates the state on the prime vault storage directly.\n            int256 totalPrimeDebt = int256(uint256(primeVaultState.totalDebt));\n            int256 newTotalDebt = totalPrimeDebt.add(netPrimeDebtChange);\n            // Set the total debt to the storage value\n            primeVaultState.totalDebt = newTotalDebt.toUint().toUint80();\n        }\n    }\n```\n