Initial modification of code to support the performance counters.
Seems the code is not as modular as initially thought.