refactor query