Bug in for loop