replaced each with reduce in function average