     /// Existing elements are moved out of the way.
     /// You are expected to fill the gap by initializing all slots in it after calling this method.
     /// Note that all previously calculated buffer indexes are invalidated by this method.
-    private func openGapAt(index: Int, length count: Int) {
+    private func openGapAt(index: Int, length: Int) {
         assert(index >= 0 && index <= self.count)
-        assert(self.count + count <= capacity)
-        guard count > 0 else { return }
-        manager.withUnsafeMutablePointers { hp, p in
-            let h = hp.memory
-            let i = h.bufferIndexForDequeIndex(index)
-            if index >= (h.count + 1) / 2 {
-                // Make room by sliding elements at/after index to the right
-                let end = h.start + h.count <= h.capacity ? h.start + h.count : h.start + h.count - h.capacity
-                if i <= end { // Elements after index are not yet wrapped
-                    if end + count <= h.capacity { // Neither gap nor elements after it will be wrapped
-                        // ....ABCD̲EF......
-                        p.advancedBy(i + count).moveInitializeBackwardFrom(p.advancedBy(i), count: end - i)
-                        // ....ABC.̲..DEF...
-                    }
-                    else if i + count <= h.capacity { // Elements after gap will be wrapped
-                        // .........ABCD̲EF. (count = 3)
-                        p.moveInitializeFrom(p.advancedBy(h.capacity - count), count: end + count - h.capacity)
-                        // EF.......ABCD̲...
-                        p.advancedBy(i + count).moveInitializeBackwardFrom(p.advancedBy(i), count: h.capacity - i - count)
-                        // EF.......ABC.̲..D
-                    }
-                    else { // Gap will be wrapped
-                        // .........ABCD̲EF. (count = 5)
-                        p.advancedBy(i + count - h.capacity).moveInitializeFrom(p.advancedBy(i), count: end - i)
-                        // .DEF.....ABC.̲...
-                    }
-                }
-                else { // Elements after index are already wrapped
-                    if i + count <= h.capacity { // Gap will not be wrapped
-                        // F.......ABCD̲E (count = 1)
-                        p.advancedBy(count).moveInitializeBackwardFrom(p, count: end)
-                        // .F......ABCD̲E
-                        p.moveInitializeFrom(p.advancedBy(h.capacity - count), count: count)
-                        // EF......ABCD̲.
-                        p.advancedBy(i + count).moveInitializeBackwardFrom(p.advancedBy(i), count: h.capacity - i - count)
-                        // EF......ABC.̲D
-                    }
-                    else { // Gap will be wrapped
-                        // F.......ABCD̲E (count = 3)
-                        p.advancedBy(count).moveInitializeBackwardFrom(p, count: end)
-                        // ...F....ABCD̲E
-                        p.advancedBy(i + count - h.capacity).moveInitializeFrom(p.advancedBy(i), count: h.capacity - i)
-                        // .DEF....ABC.̲.
-                    }
-                }
-                hp.memory.count += count
-            }
-            else {
-                // Make room by sliding elements before index to the left, updating `start`.
-                if i >= h.start { // Elements before index are not yet wrapped.
-                    if h.start >= count { // Neither gap nor elements before it will be wrapped.
-                        // ....ABCD̲EF...
-                        p.advancedBy(h.start - count).moveInitializeFrom(p.advancedBy(h.start), count: i - h.start)
-                        // .ABC...D̲EF...
-                    }
-                    else if i >= count { // Elements before the gap will be wrapped.
-                        // ..ABCD̲EF....
-                        p.advancedBy(h.capacity + h.start - count).moveInitializeFrom(p.advancedBy(h.start), count: count - h.start)
-                        // ...BCD̲EF...A
-                        p.moveInitializeFrom(p.advancedBy(count), count: i - count)
-                        // BC...D̲EF...A
-                    }
-                    else { // Gap will be wrapped
-                        // .ABCD̲EF....... (count = 5)
-                        p.advancedBy(h.capacity + h.start - count).moveInitializeFrom(p.advancedBy(h.start), count: i - h.start)
-                        // ....D̲EF...ABC.
-                    }
-                }
-                else { // Elements before index are already wrapped.
-                    if i >= count { // Gap will not be wrapped.
-                        // BCD̲EF......A (count = 1)
-                        p.advancedBy(h.start - count).moveInitializeFrom(p.advancedBy(h.start), count: h.capacity - h.start)
-                        // BCD̲EF.....A.
-                        p.advancedBy(h.capacity - count).moveInitializeFrom(p, count: count)
-                        // .CD̲EF.....AB
-                        p.moveInitializeFrom(p.advancedBy(i - count), count: i - count)
-                        // C.D̲EF.....AB
-                    }
-                    else { // Gap will be wrapped.
-                        // CD̲EF......AB
-                        p.advancedBy(h.start - count).moveInitializeFrom(p.advancedBy(h.start), count: h.capacity - h.start)
-                        // CD̲EF...AB...
-                        p.advancedBy(h.capacity - count).moveInitializeFrom(p, count: i)
-                        // .D̲EF...ABC..
-                    }
-                }
-                hp.memory.start = h.start < count ? h.capacity + h.start - count : h.start - count
-                hp.memory.count += count
-            }
+        assert(count + length <= capacity)
+        guard length > 0 else { return }
+        let i = bufferIndexForDequeIndex(index)
+        if index >= (count + 1) / 2 {
+            // Make room by sliding elements at/after index to the right
+            let end = start + count <= capacity ? start + count : start + count - capacity
+            if i <= end { // Elements after index are not yet wrapped
+                if end + length <= capacity { // Neither gap nor elements after it will be wrapped
+                    // ....ABCD̲EF......
+                    elements.advancedBy(i + length).moveInitializeBackwardFrom(elements.advancedBy(i), count: end - i)
+                    // ....ABC.̲..DEF...
+                }
+                else if i + length <= capacity { // Elements after gap will be wrapped
+                    // .........ABCD̲EF. (count = 3)
+                    elements.moveInitializeFrom(elements.advancedBy(capacity - length), count: end + length - capacity)
+                    // EF.......ABCD̲...
+                    elements.advancedBy(i + length).moveInitializeBackwardFrom(elements.advancedBy(i), count: capacity - i - length)
+                    // EF.......ABC.̲..D
+                }
+                else { // Gap will be wrapped
+                    // .........ABCD̲EF. (count = 5)
+                    elements.advancedBy(i + length - capacity).moveInitializeFrom(elements.advancedBy(i), count: end - i)
+                    // .DEF.....ABC.̲...
+                }
+            }
+            else { // Elements after index are already wrapped
+                if i + length <= capacity { // Gap will not be wrapped
+                    // F.......ABCD̲E (count = 1)
+                    elements.advancedBy(length).moveInitializeBackwardFrom(elements, count: end)
+                    // .F......ABCD̲E
+                    elements.moveInitializeFrom(elements.advancedBy(capacity - length), count: length)
+                    // EF......ABCD̲.
+                    elements.advancedBy(i + length).moveInitializeBackwardFrom(elements.advancedBy(i), count: capacity - i - length)
+                    // EF......ABC.̲D
+                }
+                else { // Gap will be wrapped
+                    // F.......ABCD̲E (count = 3)
+                    elements.advancedBy(length).moveInitializeBackwardFrom(elements, count: end)
+                    // ...F....ABCD̲E
+                    elements.advancedBy(i + length - capacity).moveInitializeFrom(elements.advancedBy(i), count: capacity - i)
+                    // .DEF....ABC.̲.
+                }
+            }
+            count += length
+        }
+        else {
+            // Make room by sliding elements before index to the left, updating `start`.
+            if i >= start { // Elements before index are not yet wrapped.
+                if start >= length { // Neither gap nor elements before it will be wrapped.
+                    // ....ABCD̲EF...
+                    elements.advancedBy(start - length).moveInitializeFrom(elements.advancedBy(start), count: i - start)
+                    // .ABC...D̲EF...
+                }
+                else if i >= length { // Elements before the gap will be wrapped.
+                    // ..ABCD̲EF....
+                    elements.advancedBy(capacity + start - length).moveInitializeFrom(elements.advancedBy(start), count: length - start)
+                    // ...BCD̲EF...A
+                    elements.moveInitializeFrom(elements.advancedBy(length), count: i - length)
+                    // BC...D̲EF...A
+                }
+                else { // Gap will be wrapped
+                    // .ABCD̲EF....... (count = 5)
+                    elements.advancedBy(capacity + start - length).moveInitializeFrom(elements.advancedBy(start), count: i - start)
+                    // ....D̲EF...ABC.
+                }
+            }
+            else { // Elements before index are already wrapped.
+                if i >= length { // Gap will not be wrapped.
+                    // BCD̲EF......A (count = 1)
+                    elements.advancedBy(start - length).moveInitializeFrom(elements.advancedBy(start), count: capacity - start)
+                    // BCD̲EF.....A.
+                    elements.advancedBy(capacity - length).moveInitializeFrom(elements, count: length)
+                    // .CD̲EF.....AB
+                    elements.moveInitializeFrom(elements.advancedBy(i - length), count: i - length)
+                    // C.D̲EF.....AB
+                }
+                else { // Gap will be wrapped.
+                    // CD̲EF......AB
+                    elements.advancedBy(start - length).moveInitializeFrom(elements.advancedBy(start), count: capacity - start)
+                    // CD̲EF...AB...
+                    elements.advancedBy(capacity - length).moveInitializeFrom(elements, count: i)
+                    // .D̲EF...ABC..
+                }
+            }
+            start = start < length ? capacity + start - length : start - length
+            count += length
         }
     }
 
     internal func insert(element: Element, at index: Int) {
         precondition(index >= 0 && index <= count && !isFull)
         openGapAt(index, length: 1)
-        let i = manager.value.bufferIndexForDequeIndex(index)
-        manager.withUnsafeMutablePointerToElements { p in
-            p.advancedBy(i).initialize(element)
-        }
+        let i = bufferIndexForDequeIndex(index)
+        elements.advancedBy(i).initialize(element)
     }
 
     internal func insertContentsOf(buffer: DequeBuffer, at index: Int) {
-        self.insertContentsOf(buffer, subRange: Range(start: 0, end: buffer.count), at: index)
+        self.insertContentsOf(buffer, subRange: 0 ..< buffer.count, at: index)
     }
 
     internal func insertContentsOf(buffer: DequeBuffer, subRange: Range<Int>, at index: Int) {

        assert(subRange.startIndex >= 0 && subRange.endIndex <= buffer.count)
        guard subRange.count > 0 else { return }
        openGapAt(index, length: subRange.count)
        self.manager.withUnsafeMutablePointers { dhp, dp in
            buffer.manager.withUnsafeMutablePointers { shp, sp in
                let dh = dhp.memory
                let sh = shp.memory

                let dstStart = dh.bufferIndexForDequeIndex(index)
                let srcStart = sh.bufferIndexForDequeIndex(subRange.startIndex)

                let srcCount = subRange.count

                let dstEnd = dh.bufferIndexForDequeIndex(index + srcCount)
                let srcEnd = sh.bufferIndexForDequeIndex(subRange.endIndex)


                if srcStart < srcEnd && dstStart < dstEnd {
                    dp.advancedBy(dstStart).initializeFrom(sp.advancedBy(srcStart), count: srcCount)
                }
                else if srcStart < srcEnd {
                    let t = dh.capacity - dstStart
                    dp.advancedBy(dstStart).initializeFrom(sp.advancedBy(srcStart), count: t)
                    dp.initializeFrom(sp.advancedBy(srcStart + t), count: srcCount - t)
                }
                else if dstStart < dstEnd {
                    let t = sh.capacity - srcStart
                    dp.advancedBy(dstStart).initializeFrom(sp.advancedBy(srcStart), count: t)
                    dp.advancedBy(dstStart + t).initializeFrom(sp, count: srcCount - t)
                }
                else {
                    let st = sh.capacity - srcStart
                    let dt = dh.capacity - dstStart

                    if dt < st {
                        dp.advancedBy(dstStart).initializeFrom(sp.advancedBy(srcStart), count: dt)
                        dp.initializeFrom(sp.advancedBy(srcStart + dt), count: st - dt)
                        dp.advancedBy(st - dt).initializeFrom(sp, count: srcCount - st)
                    }
                    else if dt > st {
                        dp.advancedBy(dstStart).initializeFrom(sp.advancedBy(srcStart), count: st)
                        dp.advancedBy(dstStart + st).initializeFrom(sp, count: dt - st)
                        dp.initializeFrom(sp.advancedBy(dt - st), count: srcCount - dt)
                    }
                    else {
                        dp.advancedBy(dstStart).initializeFrom(sp.advancedBy(srcStart), count: st)
                        dp.initializeFrom(sp, count: srcCount - st)
                    }
                }
            }
        }
    }


    /// Initialize a deque of `count` elements, each initialized to `repeatedValue`.
    public init(count: Int, repeatedValue: Element) {
        buffer = DequeBuffer(count: count, repeatedValue: repeatedValue)
    }
}

