Changed position of flag evaluation as proposed by Phil Hoffman