I haven't tested this but it looks like your checking %n but inc-ing %i.