Refactor QueryGenerator code