Added missing var scope in for loops