       // We use the index here as the identifier would have changed. The index
       // gives us a good guess for finding the corresponding segment in the new
       // trip.
-      let segmentIndex = SegmentCardsInfo.cardsInfo(ofCardAtIndex: currentPageIndex, in: segmentCards)?.segmentIndex
-      let newSegment = cardSegments.first(where: { $0.index == segmentIndex }) ?? cardSegments.first!
+      let cardInfo = SegmentCardsInfo.cardsInfo(ofCardAtIndex: currentPageIndex, in: segmentCards)
+      let newSegment = cardSegments.first(where: { $0.index == cardInfo?.segmentIndex }) ?? cardSegments.first!
       
       do {
         let newCard = try TKUITripModeByModeCard(

  fileprivate struct SegmentCardsInfo {
    let segmentIndex: Int
    let segmentIdentifier: String?
    let cards: [(TGCard, TKUISegmentMode)]
    let cardsRange: Range<Int>
    

        mapManager: tripMapManager
      )
      let range = previous.1 ..< previous.1 + cards.count
      let info = SegmentCardsInfo(segmentIndex: segment.index, segmentIdentifier: identifier, segmentMode: mode, cards: cards, cardsRange: range)
      return (previous.0 + [info], range.upperBound)
    }.0

