     fileprivate func myersDiffTraces(
         to: Self,
         isEqual: (Iterator.Element, Iterator.Element) -> Bool
-    ) -> [Trace] {
-
+        ) -> [Trace] {
+
+        // fromCount is N, N is the number of from array
         let fromCount = Int(count)
+        // toCount is M, M is the number of to array
         let toCount = Int(to.count)
         var traces = Array<Trace>()
 
         let max = fromCount + toCount // this is arbitrary, maximum difference between from and to. N+M assures that this algorithm always finds from diff
 
-        var vertices = Array(repeating: -1, count: 2 * Int(max) + 1) // from [0...2*max], it is -max...max in the whitepaper
-
-        vertices[max + 1] = 0
-
+        var vertices = Array(repeating: -1, count: max + 1) // from [0...N+M], it is -M...N in the whitepaper
+        vertices[toCount + 1] = 0
+
+        // D-patch: numberOfDifferences is D
         for numberOfDifferences in 0 ... max {
             for k in stride(from: (-numberOfDifferences), through: numberOfDifferences, by: 2) {
 
-                let index = k + max
+                guard k >= -toCount && k <= fromCount else {
+                    continue
+                }
+
+                let index = k + toCount
                 let traceStep = TraceStep(D: numberOfDifferences, k: k, previousX: vertices.value(at: index - 1), nextX: vertices.value(at: index + 1))
                 if let trace = bound(trace: nextTrace(traceStep), maxX: fromCount, maxY: toCount) {
                     var x = trace.to.x

        let D = traceStep.D

        if traceType == .insertion {
            let x = traceStep.nextX!
            return Trace(from: Point(x: x, y: x - k - 1), to: Point(x: x, y: x - k), D: D)
        } else {
            let x = traceStep.previousX! + 1
            return Trace(from: Point(x: x - 1, y: x - k), to: Point(x: x, y: x - k), D: D)
        }
    }

        }
    }
}

