Add support for query_id, and fixes a few cases where programmer error can