Flags routines optimized

The complexity of flag routines were reduced in order to improve speed