Optimised a few for() loops, caching array lengths - better for Chrome V8 engine