           if JetInsert.isInsert(elem)
             docIndex += elem.getLength()
           else
-            if elem.start <= elemC.start && elem.end >= elemC.end # If this retain contains elemC retain
+            if elem.start <= elemC.start && elem.end > elemC.start
+              take = Math.min(elemC.getLength(), elem.getLength())
               docIndex += elemC.start - elem.start
+              decomposed = decomposed.concat(new JetRetain(docIndex, docIndex + take, decomposeAttributes(elem, elemC)))
+              docIndex += take
+              deltaA.deltas.splice(0, deltasSeen)
+              advance(elem, elemC.start - elem.start + take, "A")
+              advance(elemC, take, "C")
               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
+          deltasSeen++
     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

      return count

    advance = (elem, advanceBy, whichElem) ->
      if advanceBy == elem.getLength()
        deltaA.deltas.shift() if whichElem == "A"
        deltaC.deltas.shift() if whichElem == "C"
      else
        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) ->
