Modified avg function