cleaning up averaging function