Always initialize your variables. I would prefer to declare your variables only when you're ready to initialize and or use them, which also helps remind you of that rule. That might help you here.
You also might want to think about how many times your for loop runs, but that's probably not your current problem. Basically, you've made a couple changes from your original attempt that made things worse.