   popUndoStack: (currentSnapshot) ->
     snapshotAbove = null
     snapshotBelow = null
-    spliceIndex = -1
+    spliceIndex = null
     withinGroup = false
     invertedChanges = []
-    for entry, i in @undoStack by -1
-      if entry instanceof GroupStart
-        if withinGroup
-          snapshotBelow = entry.snapshot
-          spliceIndex = i
-          break
-        else
-          return false
-      else if entry instanceof GroupEnd
-        if withinGroup
-          throw new Error("Invalid undo stack state")
-        else
-          snapshotAbove = entry.snapshot
-          withinGroup = true
-      else unless entry instanceof Checkpoint
-        invertedChanges.push(@delegate.invertChange(entry))
-        unless withinGroup
-          spliceIndex = i
-          break
-
-    if spliceIndex >= 0
+
+    for entry, i in @undoStack by -1
+      break if spliceIndex?
+
+      switch entry.constructor
+        when GroupStart
+          if withinGroup
+            snapshotBelow = entry.snapshot
+            spliceIndex = i
+          else
+            return false
+        when GroupEnd
+          if withinGroup
+            throw new Error("Invalid undo stack state")
+          else
+            snapshotAbove = entry.snapshot
+            withinGroup = true
+        when Checkpoint
+        else
+          invertedChanges.push(@delegate.invertChange(entry))
+          unless withinGroup
+            spliceIndex = i
+
+    if spliceIndex?
       _.defaults(snapshotAbove, currentSnapshot) if snapshotAbove?
       @redoStack.push(@undoStack.splice(spliceIndex).reverse()...)
       {

  popRedoStack: (currentSnapshot) ->
    snapshotAbove = null
    snapshotBelow = null
    spliceIndex = -1
    withinGroup = false
    changes = []
    for entry, i in @redoStack by -1
      if entry instanceof GroupEnd
        if withinGroup
          snapshotBelow = entry.snapshot
          spliceIndex = i
          break
        else
          return false
      else if entry instanceof GroupStart
        if withinGroup
          throw new Error("Invalid redo stack state")
        else
          snapshotAbove = entry.snapshot
          withinGroup = true
      else unless entry instanceof Checkpoint
        changes.push(entry)
        unless withinGroup
          spliceIndex = i
          break

    while @redoStack[spliceIndex - 1] instanceof Checkpoint
      spliceIndex--

    if spliceIndex >= 0
      _.defaults(snapshotAbove, currentSnapshot) if snapshotAbove?
      @undoStack.push(@redoStack.splice(spliceIndex).reverse()...)
      {


  groupChangesSinceCheckpoint: (checkpointId, endSnapshot, deleteCheckpoint=false) ->
    withinGroup = false
    checkpointIndex = null
    startSnapshot = null
    changesSinceCheckpoint = []

    for entry, i in @undoStack by -1
      break if checkpointIndex?

      switch entry.constructor
        when GroupEnd
          withinGroup = true
        when GroupStart
          if withinGroup
            withinGroup = false
          else
            return false
        when Checkpoint
          if entry.id is checkpointId
            checkpointIndex = i
            startSnapshot = entry.snapshot
        else
          changesSinceCheckpoint.unshift(entry)

    if checkpointIndex?
      if changesSinceCheckpoint.length > 0
        spliceIndex = checkpointIndex
        spliceIndex++ unless deleteCheckpoint
