refactor the queries