     /// Destroy elements in the range (index ..< index + count) and collapse the gap by moving remaining elements.
     /// Note that all previously calculated buffer indexes are invalidated by this method.
     private func removeRange(range: Range<Int>) {
-        let index = range.startIndex
-        let count = range.count
         assert(range.startIndex >= 0)
         assert(range.endIndex <= self.count)
         guard range.count > 0 else { return }
-        manager.withUnsafeMutablePointers { hp, p in
-            let h = hp.memory
-            let i = h.bufferIndexForDequeIndex(index)
-            let j = i + count <= h.capacity ? i + count : i + count - h.capacity
-
-            // Destroy items in collapsed range
-            if i <= j {
-                // ....ABC̲D̲E̲FG...
-                p.advancedBy(i).destroy(count)
-                // ....AB...FG...
-            }
-            else {
-                // D̲E̲FG.......ABC̲
-                p.advancedBy(i).destroy(h.capacity - i)
-                // D̲E̲FG.......AB.
-                p.destroy(j)
-                // ..FG.......AB.
-            }
-
-            if h.count - index - count < index {
-                let end = h.start + h.count < h.capacity ? h.start + h.count : h.start + h.count - h.capacity
-
-                // Slide trailing items to the left
-                if i <= end { // No wrap anywhere after start of collapsed range
-                    // ....AB.̲..CD...
-                    p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + count), count: end - i - count)
-                    // ....ABC̲D......
-                }
-                else if i + count > h.capacity { // Collapsed range is wrapped
-                    if end <= count { // Result will not be wrapped
-                        // .CD......AB.̲..
-                        p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + count - h.capacity), count: h.capacity + end - i - count)
-                        // .........ABC̲D.
-                    }
-                    else { // Result will remain wrapped
-                        // .CDEFG...AB.̲..
-                        p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + count - h.capacity), count: h.capacity - i)
-                        // ....FG...ABC̲DE
-                        p.moveInitializeFrom(p.advancedBy(count), count: end - count)
-                        // FG.......ABC̲DE
-                    }
-                }
-                else { // Wrap is after collapsed range
-                    if end <= count { // Result will not be wrapped
-                        // D.......AB.̲..C
-                        p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + count), count: h.capacity - i - count)
-                        // D.......ABC̲...
-                        p.advancedBy(h.capacity - count).moveInitializeFrom(p, count: end)
-                        // ........ABC̲D..
-                    }
-                    else { // Result will remain wrapped
-                        // DEFG....AB.̲..C
-                        p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + count), count: h.capacity - i - count)
-                        // DEFG....ABC̲...
-                        p.advancedBy(h.capacity - count).moveInitializeFrom(p, count: count)
-                        // ...G....ABC̲DEF
-                        p.moveInitializeFrom(p.advancedBy(count), count: end - count)
-                        // G.......ABC̲DEF
-                    }
-                }
-                hp.memory.count -= count
-            }
-            else {
-                // Slide preceding items to the right
-                if j >= h.start { // No wrap anywhere before end of collapsed range
-                    // ...AB...C̲D...
-                    p.advancedBy(h.start + count).moveInitializeBackwardFrom(p.advancedBy(h.start), count: j - h.start - count)
-                    // ......ABC̲D...
-                }
-                else if j < count { // Collapsed range is wrapped
-                    if  h.start + count >= h.capacity  { // Result will not be wrapped
-                        // ...C̲D.....AB..
-                        p.advancedBy(h.start + count - h.capacity).moveInitializeFrom(p.advancedBy(h.start), count: h.capacity + j - h.start - count)
-                        // .ABC̲D.........
-                    }
-                    else { // Result will remain wrapped
-                        // ..E̲F.....ABCD..
-                        p.moveInitializeFrom(p.advancedBy(h.capacity - count), count: j)
-                        // CDE̲F.....AB....
-                        p.advancedBy(h.start + count).moveInitializeBackwardFrom(p.advancedBy(h.start), count: h.capacity - h.start - count)
-                        // CDE̲F.........AB
-                    }
-                }
-                else { // Wrap is before collapsed range
-                    if h.capacity - h.start <= count { // Result will not be wrapped
-                        // CD...E̲F.....AB
-                        p.advancedBy(count).moveInitializeBackwardFrom(p, count: j - count)
-                        // ...CDE̲F.....AB
-                        p.advancedBy(h.start + count - h.capacity).moveInitializeFrom(p.advancedBy(h.start), count: h.capacity - h.start)
-                        // .ABCDE̲F.......
-                    }
-                    else { // Result will remain wrapped
-                        // EF...G̲H...ABCD
-                        p.advancedBy(count).moveInitializeBackwardFrom(p, count: j - count)
-                        // ...EFG̲H...ABCD
-                        p.moveInitializeFrom(p.advancedBy(h.capacity) - count, count: count)
-                        // BCDEFG̲H...A...
-                        p.advancedBy(h.start + count).moveInitializeBackwardFrom(p.advancedBy(h.start), count: h.capacity - h.start - count)
-                        // BCDEFG̲H......A
-                    }
-                }
-                hp.memory.start = (h.start + count < h.capacity ? h.start + count : h.start + count - h.capacity)
-                hp.memory.count -= count
-            }
+        let rc = range.count
+        let p = elements
+        let i = bufferIndexForDequeIndex(range.startIndex)
+        let j = i + rc <= capacity ? i + rc : i + rc - capacity
+
+        // Destroy items in collapsed range
+        if i <= j {
+            // ....ABC̲D̲E̲FG...
+            p.advancedBy(i).destroy(rc)
+            // ....AB...FG...
+        }
+        else {
+            // D̲E̲FG.......ABC̲
+            p.advancedBy(i).destroy(capacity - i)
+            // D̲E̲FG.......AB.
+            p.destroy(j)
+            // ..FG.......AB.
+        }
+
+        if count - range.startIndex - rc < range.startIndex {
+            let end = start + count < capacity ? start + count : start + count - capacity
+
+            // Slide trailing items to the left
+            if i <= end { // No wrap anywhere after start of collapsed range
+                // ....AB.̲..CD...
+                p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + rc), count: end - i - rc)
+                // ....ABC̲D......
+            }
+            else if i + rc > capacity { // Collapsed range is wrapped
+                if end <= rc { // Result will not be wrapped
+                    // .CD......AB.̲..
+                    p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + rc - capacity), count: capacity + end - i - rc)
+                    // .........ABC̲D.
+                }
+                else { // Result will remain wrapped
+                    // .CDEFG...AB.̲..
+                    p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + rc - capacity), count: capacity - i)
+                    // ....FG...ABC̲DE
+                    p.moveInitializeFrom(p.advancedBy(rc), count: end - rc)
+                    // FG.......ABC̲DE
+                }
+            }
+            else { // Wrap is after collapsed range
+                if end <= rc { // Result will not be wrapped
+                    // D.......AB.̲..C
+                    p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + rc), count: capacity - i - rc)
+                    // D.......ABC̲...
+                    p.advancedBy(capacity - rc).moveInitializeFrom(p, count: end)
+                    // ........ABC̲D..
+                }
+                else { // Result will remain wrapped
+                    // DEFG....AB.̲..C
+                    p.advancedBy(i).moveInitializeFrom(p.advancedBy(i + rc), count: capacity - i - rc)
+                    // DEFG....ABC̲...
+                    p.advancedBy(capacity - rc).moveInitializeFrom(p, count: rc)
+                    // ...G....ABC̲DEF
+                    p.moveInitializeFrom(p.advancedBy(rc), count: end - rc)
+                    // G.......ABC̲DEF
+                }
+            }
+            count -= rc
+        }
+        else {
+            // Slide preceding items to the right
+            if j >= start { // No wrap anywhere before end of collapsed range
+                // ...AB...C̲D...
+                p.advancedBy(start + rc).moveInitializeBackwardFrom(p.advancedBy(start), count: j - start - rc)
+                // ......ABC̲D...
+            }
+            else if j < rc { // Collapsed range is wrapped
+                if  start + rc >= capacity  { // Result will not be wrapped
+                    // ...C̲D.....AB..
+                    p.advancedBy(start + rc - capacity).moveInitializeFrom(p.advancedBy(start), count: capacity + j - start - rc)
+                    // .ABC̲D.........
+                }
+                else { // Result will remain wrapped
+                    // ..E̲F.....ABCD..
+                    p.moveInitializeFrom(p.advancedBy(capacity - rc), count: j)
+                    // CDE̲F.....AB....
+                    p.advancedBy(start + rc).moveInitializeBackwardFrom(p.advancedBy(start), count: capacity - start - rc)
+                    // CDE̲F.........AB
+                }
+            }
+            else { // Wrap is before collapsed range
+                if capacity - start <= rc { // Result will not be wrapped
+                    // CD...E̲F.....AB
+                    p.advancedBy(rc).moveInitializeBackwardFrom(p, count: j - rc)
+                    // ...CDE̲F.....AB
+                    p.advancedBy(start + rc - capacity).moveInitializeFrom(p.advancedBy(start), count: capacity - start)
+                    // .ABCDE̲F.......
+                }
+                else { // Result will remain wrapped
+                    // EF...G̲H...ABCD
+                    p.advancedBy(rc).moveInitializeBackwardFrom(p, count: j - rc)
+                    // ...EFG̲H...ABCD
+                    p.moveInitializeFrom(p.advancedBy(capacity) - rc, count: rc)
+                    // BCDEFG̲H...A...
+                    p.advancedBy(start + rc).moveInitializeBackwardFrom(p.advancedBy(start), count: capacity - start - rc)
+                    // BCDEFG̲H......A
+                }
+            }
+            start = (start + rc < capacity ? start + rc : start + rc - capacity)
+            count -= rc
         }
     }
 
     internal func replaceRange<C: CollectionType where C.Generator.Element == Element>(range: Range<Int>, with newElements: C) {
-        let h = manager.value
         let newCount: Int = numericCast(newElements.count)
         let delta = newCount - range.count
-        assert(h.count + delta < h.capacity)
+        assert(count + delta < capacity)
         let common = min(range.count, newCount)
         if common > 0 {
-            manager.withUnsafeMutablePointerToElements { p in
-                var q = p.advancedBy(h.bufferIndexForDequeIndex(range.startIndex))
-                let limit = p.advancedBy(h.capacity)
-                var i = common
-                for element in newElements {
-                    q.memory = element
-                    q = q.successor()
-                    if q == limit { q = p }
-                    i -= 1
-                    if i == 0 { break }
-                }
+            let p = elements
+            var q = p.advancedBy(bufferIndexForDequeIndex(range.startIndex))
+            let limit = p.advancedBy(capacity)
+            var i = common
+            for element in newElements {
+                q.memory = element
+                q = q.successor()
+                if q == limit { q = p }
+                i -= 1
+                if i == 0 { break }
             }
         }
         if range.count > common {
-            removeRange(Range(start: range.startIndex + common, end: range.endIndex))
+            removeRange(range.startIndex + common ..< range.endIndex)
         }
         else if newCount > common {
             openGapAt(range.startIndex + common, length: newCount - common)
-            manager.withUnsafeMutablePointerToElements { p in
-                var q = p.advancedBy(h.bufferIndexForDequeIndex(range.startIndex + common))
-                let limit = p.advancedBy(h.capacity)
-                var i = newElements.startIndex.advancedBy(numericCast(common))
-                while i != newElements.endIndex {
-                    q.initialize(newElements[i])
-                    i = i.successor()
-                    q = q.successor()
-                    if q == limit { q = p }
-                }
-            }
-        }
-    }
-}
+            let p = elements
+            var q = p.advancedBy(bufferIndexForDequeIndex(range.startIndex + common))
+            let limit = p.advancedBy(capacity)
+            var i = newElements.startIndex.advancedBy(numericCast(common))
+            while i != newElements.endIndex {
+                q.initialize(newElements[i])
+                i = i.successor()
+                q = q.successor()
+                if q == limit { q = p }
+            }
+        }
+    }
+}

                reserveCapacity(grow(count + 1))
                capacity = buffer.capacity
            }
            var i = buffer.manager.value.bufferIndexForDequeIndex(count)
            buffer.manager.withUnsafeMutablePointerToElements { p in
                while let element = next where count < capacity {
                    p.advancedBy(i).initialize(element)
                    i += 1
                    if i == capacity { i = 0 }
                    count += 1
                    next = generator.next()
                }
            }
            buffer.count = count
        }

        assert(count + c <= capacity)
        guard c > 0 else { return }
        openGapAt(index, length: c)
        var q = elements.advancedBy(bufferIndexForDequeIndex(index))
        let limit = elements.advancedBy(capacity)
        for element in collection {
            q.initialize(element)
            q = q.successor()
            if q == limit {
                q = elements
            }
        }
    }
