Added var scoping to the for loops

Improved my earlier submission by fixing the out op scope i vars