Refactored statistics code into separate class with test.