Refactored tracing code