refactor query builder