Query refactor