                 throw I2CError.readOrWriteError(status, errno)
             }
             
-            if let buf = readBuf {
-                return unsafeBitCast(buf, to: [UInt8].self)
+            if let buf = readPtr {
+                let bptr = UnsafeBufferPointer(start: buf.assumingMemoryBound(to: UInt8.self), count: Int(readBytes))
+                return Array(bptr)
             }
             return []
         }

            var messages = [i2c_msg](repeating: i2c_msg(), count: messageLength)
            
            if writeLength > 0 {
                var writeBuf = unsafeBitCast(data, to: [Int8].self)
                messages[messageIndex].addr = UInt16(toAddress)
                messages[messageIndex].flags = 0 // Write mode
                messages[messageIndex].len = Int16(data.count)
                setBufferPtr(msg: &messages[messageIndex], buf: &writeBuf)
                
                messageIndex += 1
            }
            
            var readBuf: [Int8]?
            if readBytes > 0 {
                readBuf = [Int8](repeating: 0, count: Int(readBytes))
                messages[messageIndex].addr = UInt16(toAddress)
                messages[messageIndex].flags = UInt16(I2C_M_RD)
                messages[messageIndex].len = Int16(readBytes)
                setBufferPtr(msg: &messages[messageIndex], buf: &readBuf!)
            }
            
            //print("writing...", messages)

            close(i2c)
        }
        
        public func write(toAddress: UInt8, data: [UInt8], readBytes: UInt32) throws -> [UInt8] {
            
            // send only start, stop
