refactoring for moving averages