NB: Simple move of flag setup outside of loop. Not really related to issue, but sneaking in.