Port many foreach's to for's.
A big amount. I tried checking the context in all of these; most should be very straightforward, but there are some that needed some extra work. Some hundreds still left (some of them might need a bit more thought, too).