Moved flag check into correct location