Improved the calculation logic.