Added flag checking to queries