refactor query.py