Some bug fixes and query tracing