fix algorithm, add discount, small refactors