refactors instrumentation to be more efficient and adds better unit tests.