did a bit of refactoring, plus improved the running average calculation