refactor calculation logic