Refactor instrumentation code