Factored out 'joinSegments'.