Improved query building.