Fix bug in for loop