added proper flag checking