Move trace_level helper functions out of loop