Finalized discounts algorithm. Misc fixes.