Improvements to trace code.
Not quite done yet.