Refactored calculation code