calc algo imroved, small refactoring