Query builder refactor