Better query error checking