       return count
 
     advance = (elem, advanceBy, whichElem) ->
-      console.assert JetDelta.isInsert(elem), "advance expected insert but got retain: #{elem}"
       if advanceBy == elem.getLength()
         deltaA.deltas.shift() if whichElem == "A"
         deltaC.deltas.shift() if whichElem == "C"
       else
-        elem.text = elem.text.substring(advanceBy)
+        if JetDelta.isInsert(elem)
+          elem.text = elem.text.substring(advanceBy)
+        else
+          console.assert JetDelta.isRetain(elem)
+          elem.start += advanceBy
 
     # XXX: Define this on JetDelta?
     decomposeAttributes = (elemA, elemC) ->

          if JetInsert.isInsert(elem)
            docIndex += elem.getLength()
          else
            if elem.start <= elemC.start && elem.end >= elemC.end # If this retain contains elemC retain
              docIndex += elemC.start - elem.start
              break
            else
              docIndex += elem.getLength()
          deltasSeen += 1
        decomposed = decomposed.concat(new JetRetain(docIndex, docIndex + elemC.getLength(), decomposeAttributes(elemA, elemC)))
        docIndex += elemC.getLength()
        deltaC.deltas.shift()
        deltaA.deltas.splice(0, deltasSeen)
        if _.first(deltaA.deltas).end == elemC.end
          deltaA.deltas.shift()
        else
          _.first(deltaA.deltas).start = elemC.end
    while (deltaC.deltas.length > 0)
      elemC = _.first(deltaC.deltas)
      console.assert(JetDelta.isInsert(elemC), "Received Retain when expecting insert: #{elemC}")

root.JetRetain = JetRetain
root.JetInsert = JetInsert
root.JetDelta = JetDelta
root.JetSync = JetSync