Refactoring queryGenerator to make it more ... DRY

*WORK IN PROGRESS*