Section 21. If all the claims against the property covered by the lien were ascertained at the time of ordering the sale and if the proceeds of the sale are sufficient therefor, the court may order the officer to distribute them, after deducting all lawful charges and expenses of such officer, to and among the several creditors to the amount of their respective debts, with interest, or, if insufficient, to distribute the same among the lien creditors pursuant to this chapter in proportion to the amount due to each, regardless of the date upon which each such lien creditor filed a notice of contract, except that claims of lien creditors under sections 2C and 2D shall be paid only after payment in full of the claims of other lien creditors pursuant to this chapter. If all the claims were not ascertained at the time of ordering the sale or other sufficient cause is shown, the court may order the officer to bring the proceeds of the sale into court to be disposed of according to its decree. If the whole cannot be conveniently distributed at one time, the court may make successive orders of distribution. If there is a surplus of the proceeds of the sale after making all payments before mentioned, it shall be paid over to the owner of the property; but, before it is so paid over, it may be attached or taken on execution in like manner as proceeds from a sale on execution. Notwithstanding the foregoing, proceeds of the sale shall be distributed to mortgagees entitled to priority pursuant to this chapter in the amounts given priority thereunder, before proceeds are distributed to creditors whose liens arise under this chapter.