fixed refactoring of query.py