refactored tracing midfunc thanks to @khellang