fixed a for loop that erased elements while iterating