updated flag logic