doing some house cleaning with the statistics code

I'm starting the process of refactoring pretty much all of the code
that generates the statistics. Still a lot remains to be done.