Fixes to multithreaded query code