Made flag detection more dynamic.