Move all collection adjustments outside of the collection's loop. Replace all instances of direct manipulation of collections with their correct APIs.