Refactored calculate_cost_total