(Mostly-automated) code clean-up

Just some code cleanup; most of them were automated using Eclipse, but these were manual changes:
- Variables were moved inside smaller scopes as much as possible
- Iterators were replaced with 'for each' loops where possible