Fix logical error when removing elements inside a flag array.