 class DataWithPointer {
 
     let bitOrder: BitOrder
-    let bitVector: CFBitVector
-    var index: Int
-    var bitShift: Int
+    private let bitArray: [UInt8]
+    var index: Int = 0
+    var bitMask: UInt8
 
     init(data: Data, bitOrder: BitOrder) {
-        self.index = 0
-        self.bitShift = 0
         self.bitOrder = bitOrder
-        self.bitVector = CFBitVectorCreate(kCFAllocatorDefault, data.toArray(type: UInt8.self), data.count * 8)
+        self.bitArray = data.toArray(type: UInt8.self)
+
+        switch self.bitOrder {
+        case .reversed:
+            self.bitMask = 1
+        case .straight:
+            self.bitMask = 128
+        }
     }
 
     func bits(count: Int) -> [UInt8] {


        var array: [UInt8] = Array(repeating: 0, count: count)
        for i in 0..<count {
            let currentIndex: Int
            switch self.bitOrder {
            case .straight:
                currentIndex = 8 * index + bitShift
            case .reversed:
                currentIndex = 8 * (index + 1) - bitShift - 1
            }

            array[i] = UInt8(truncatingBitPattern: CFBitVectorGetBitAtIndex(self.bitVector, currentIndex))

            self.bitShift += 1
            if self.bitShift >= 8 {
                self.bitShift = 0
                self.index += 1
            }
        }



    func intFromBits(count: Int) -> Int {
        guard count > 0 else { return 0 }

        var result = 0
        for i in 0..<count {
            let power: Int
            switch self.bitOrder {
            case .straight:
                power = count - i - 1
            case .reversed:
                power = i
            }

            let bit = self.bitArray[self.index] & self.bitMask > 0 ? 1 : 0
            result += Int(pow(Double(2), Double(power))) * bit

            switch self.bitOrder {
            case .reversed:
                if self.bitMask == 128 {
                    self.index += 1
                    self.bitMask = 1
                } else {
                    self.bitMask <<= 1
                }
            case .straight:
                if self.bitMask == 1 {
                    self.index += 1
                    self.bitMask = 128
                } else {
                    self.bitMask >>= 1
                }
            }
        }

