Refactored query builder.