I think it's correct up until the for(i=1...) line