Query related bugfixes and additions.