added mergeSegments method. seems like a hack, but it can be refactored
later.