Query bugfixes