added data averaging function