new function for averaging.