Fix some bugs with for loops