Fixes for loops using array.length multiple times