Refactoring

Offloaded a lot of the work to new functions, namely createCollector and
create totalsCollector.
More refactoring.