relocated calc_avg to its own function