Averaging code updated