I don't see an effect on entity purging at a first glance, but there's a potentially dangerous mistake in 2 lines:
...flags = ....
Flags are set with |=, not =. With '=' you're deleting all internal flags, with possibly bad consequences. For being on the safe side, always use the "set" macro that we provide.