Fix bug when removing an item from a list while iterating on the list