Cleanup, improved debug tracing, performance optimizations

Cleanup, improved debug tracing, performance optimizations