I suggest the code below. It's very close to your own but it seems to
avoid the problems.
When I tried your code out, the same problems happened for me! I concluded that this is an issue with the viewer in LilyPondTool, and sure enough, when I "view PDF with external PDF-viewer," the problem disappears, and I see all 20 line segments with both your version and mine too.
Certain aspects of your rewrite are clearer than mine--thank you!