refactoring averaging