The error must be in here. Shouldn't this be n2 + 1?