Fixed a bunch of issues with queries.