Appending lines in the right place, inside <g id='lines'>
Stable but messy; now get rid of globals