Fixed a few bugs in the creation of queries.