fix the remove-while-iterating pattern