refactor query a bit