Moved flag check to the beginning of open()