fixed bugs in for loops.