refactor some queries