Fix the average calculation function.