trying to debug mysterious for loop bug