Fixed query bugs