Fixed minor bugs in queries. Part of #2028.