calculate rest correctly; add tests