Ouch! I totally missed this at first. In general, try to avoid for loops in which you change the variable governing the loop inside. If you need to do that, use a while instead. Good that you refactored it though!