modified all for loops to use range where possible