Optimized for-loops, and other minor changes.