added last line segment, various other improvements