refactor, pass query as parameter