refactor query stuff