Increment i not n. The scary thing is that with optimizations it did sometimes work. (Note to self: start writing tests).