refactor FirstAndLastSegmentOfWay