I just noticed that you're using Math.round(). That should be Math.floor() so it never rounds up which may cause total length to exceed length of line.