refactoring of query builder