Improved query checking