Refactored parts of the stats code #103