Separate stats calculation from output formatting so it can be used for multiple output formats