Bug fixes in queries.py