Incorrect calculations, Can't found the cause.

Try subdivisons - 2
It’s i < subd - 1, not i <= subd-1