refactor queries