Bug fixes in queries