refactored math for clarity