Refactor moving average