CAB, the very good routine of gile, but I did not try to add a vertex, look at the code in execution:
1 - depending where click, that will be the vertex 1 of the polyline.
2 - depending on where that will be click the sense of vertices, 1 2 3 4 ....
me and mine code works fine, but I guess you can improve