fix scoping issue in for loops