Refactoring calculate algorithm