         case binary(Data)
         case uuid(UUID)
         case array([DataPack])
+        case undecoded_array(UndecodedArray)
         case dictionary([String: DataPack])
-        case undecoded(UndecodedObject)
-    }
-}
-
-extension DataPack.Base {
-    
-    @inlinable
-    var type: DataPackType {
-        switch self {
-        case .null: return .null
-        case .boolean: return .boolean
-        case .string: return .string
-        case .signed: return .signed
-        case .unsigned: return .unsigned
-        case .number: return .number
-        case .binary: return .binary
-        case .uuid: return .uuid
-        case .array: return .array
-        case .dictionary: return .dictionary
-        case let .undecoded(value): return value.type
-        }
-    }
-    
-    @inlinable
-    var boolValue: Bool? {
-        switch self {
-        case let .boolean(value): return value
-        case let .undecoded(value): return value.boolValue
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var int64Value: Int64? {
-        switch self {
-        case let .signed(value): return value
-        case let .undecoded(value): return value.int64Value
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var uint64Value: UInt64? {
-        switch self {
-        case let .unsigned(value): return value
-        case let .undecoded(value): return value.uint64Value
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var doubleValue: Double? {
-        switch self {
-        case let .number(value): return value
-        case let .undecoded(value): return value.doubleValue
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var string: String? {
-        switch self {
-        case let .string(value): return value
-        case let .undecoded(value): return value.string
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var binary: Data? {
-        switch self {
-        case let .binary(value): return value
-        case let .undecoded(value): return value.binary
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var uuid: UUID? {
-        switch self {
-        case let .uuid(value): return value
-        case let .undecoded(value): return value.uuid
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var array: [DataPack]? {
-        switch self {
-        case let .array(value): return value
-        case let .undecoded(value): return value.array
-        default: return nil
-        }
-    }
-    
-    @inlinable
-    var dictionary: [String: DataPack]? {
-        switch self {
-        case let .dictionary(value): return value
-        case let .undecoded(value): return value.dictionary
-        default: return nil
-        }
-    }
-}
-
-extension DataPack.Base {
-    
-    @inlinable
-    func encode(to data: inout MappedBuffer<UInt8>) {
-        switch self {
-        case .null: data.append(0x3F)
+    }
+    
+    @usableFromInline
+    let base: Base
+    
+    @inlinable
+    public init(decode data: Data) {
+        self.init(decode: data, xref: data, stack: [])
+    }
+    
+    @inlinable
+    public init(contentsOf url: URL, options: Data.ReadingOptions = []) throws {
+        try self.init(decode: Data(contentsOf: url, options: options))
+    }
+    
+    @inlinable
+    public init(contentsOfFile path: String, options: Data.ReadingOptions = []) throws {
+        try self.init(decode: Data(contentsOf: URL(fileURLWithPath: path), options: options))
+    }
+    
+    @inlinable
+    public init(_ value: Bool) {
+        self.base = .boolean(value)
+    }
+    
+    @inlinable
+    public init(_ value: String) {
+        self.base = .string(value)
+    }
+    
+    @inlinable
+    public init<S: StringProtocol>(_ value: S) {
+        self.base = .string(String(value))
+    }
+    
+    @inlinable
+    public init<T: FixedWidthInteger & SignedInteger>(_ value: T) {
+        self.base = .signed(Int64(value))
+    }
+    
+    @inlinable
+    public init<T: FixedWidthInteger & UnsignedInteger>(_ value: T) {
+        self.base = .unsigned(UInt64(value))
+    }
+    
+    @inlinable
+    public init<T: BinaryFloatingPoint>(_ value: T) {
+        self.base = .number(Double(value))
+    }
+    
+    @inlinable
+    public init(_ binary: Data) {
+        self.base = .binary(binary)
+    }
+    
+    @inlinable
+    public init(_ uuid: UUID) {
+        self.base = .uuid(uuid)
+    }
+    
+    @inlinable
+    public init<S: Sequence>(_ elements: S) where S.Element == DataPack {
+        self.base = .array(Array(elements))
+    }
+    
+    @inlinable
+    public init(_ elements: [String: DataPack]) {
+        self.base = .dictionary(elements)
+    }
+}
+
+extension DataPack: ExpressibleByNilLiteral {
+    
+    @inlinable
+    public init(nilLiteral value: Void) {
+        self.base = .null
+    }
+}
+
+extension DataPack: ExpressibleByBooleanLiteral {
+    
+    @inlinable
+    public init(booleanLiteral value: BooleanLiteralType) {
+        self.init(value)
+    }
+}
+
+extension DataPack: ExpressibleByIntegerLiteral {
+    
+    @inlinable
+    public init(integerLiteral value: IntegerLiteralType) {
+        self.init(value)
+    }
+}
+
+extension DataPack: ExpressibleByFloatLiteral {
+    
+    @inlinable
+    public init(floatLiteral value: FloatLiteralType) {
+        self.init(value)
+    }
+}
+
+extension DataPack: ExpressibleByStringLiteral {
+    
+    @inlinable
+    public init(stringLiteral value: StringLiteralType) {
+        self.init(value)
+    }
+}
+
+extension DataPack: ExpressibleByArrayLiteral {
+    
+    @inlinable
+    public init(arrayLiteral elements: DataPack ...) {
+        self.init(elements)
+    }
+}
+
+extension DataPack: ExpressibleByDictionaryLiteral {
+    
+    @inlinable
+    public init(dictionaryLiteral elements: (String, DataPack) ...) {
+        self.init(Dictionary(uniqueKeysWithValues: elements))
+    }
+}
+
+extension DataPack: CustomStringConvertible {
+    
+    @inlinable
+    public var description: String {
+        switch self.base {
+        case .null: return "nil"
+        case let .boolean(value): return "\(value)"
+        case let .string(value): return "\"\(value.escaped(asASCII: false))\""
+        case let .signed(value): return "\(value)"
+        case let .unsigned(value): return "\(value)"
+        case let .number(value): return "\(value)"
+        case let .binary(value): return "\(value)"
+        case let .uuid(value): return "\(value)"
+        case let .array(value): return "\(value)"
+        case let .undecoded_array(value): return "\(Array(value))"
+        case let .dictionary(value): return "\(value)"
+        }
+    }
+}
+
+extension DataPack: Hashable {
+    
+    @inlinable
+    public static func == (lhs: DataPack, rhs: DataPack) -> Bool {
+        switch (lhs.base, rhs.base) {
+        case (.null, .null): return true
+        case let (.boolean(lhs), .boolean(rhs)): return lhs == rhs
+        case let (.string(lhs), .string(rhs)): return lhs == rhs
+        case let (.signed(lhs), .signed(rhs)): return lhs == rhs
+        case let (.unsigned(lhs), .unsigned(rhs)): return lhs == rhs
+        case let (.number(lhs), .number(rhs)): return lhs == rhs
+        case let (.binary(lhs), .binary(rhs)): return lhs == rhs
+        case let (.uuid(lhs), .uuid(rhs)): return lhs == rhs
+        case let (.array(lhs), .array(rhs)): return lhs == rhs
+        case let (.array(lhs), .undecoded_array(rhs)): return lhs == Array(rhs)
+        case let (.undecoded_array(lhs), .array(rhs)): return Array(lhs) == rhs
+        case let (.undecoded_array(lhs), .undecoded_array(rhs)): return Array(lhs) == Array(rhs)
+        case let (.dictionary(lhs), .dictionary(rhs)): return lhs == rhs
+        default: return false
+        }
+    }
+    
+    @inlinable
+    public func hash(into hasher: inout Hasher) {
+        hasher.combine(type)
+        switch self.base {
+        case let .boolean(value): hasher.combine(value)
+        case let .string(value): hasher.combine(value)
+        case let .signed(value): hasher.combine(value)
+        case let .unsigned(value): hasher.combine(value)
+        case let .number(value): hasher.combine(value)
+        case let .binary(value): hasher.combine(value)
+        case let .uuid(value): hasher.combine(value)
+        case let .array(value): hasher.combine(value)
+        case let .undecoded_array(value): hasher.combine(Array(value))
+        case let .dictionary(value): hasher.combine(value)
+        default: break
+        }
+    }
+}
+
+extension DataPack {
+    
+    @inlinable
+    init(decode data: Data, xref: Data, stack: Set<Int>) {
+        
+        let type = data.first
+        let data = data.dropFirst()
+        
+        switch type {
+        case 0x3F: self.base = .null
+        case 0x66: self.base = .boolean(false)
+        case 0x74: self.base = .boolean(true)
+        case 0x69:
+            
+            switch data.count {
+            case 1: self.base = .signed(Int64(data.load(as: Int8.self)))
+            case 2: self.base = .signed(Int64(Int16(bigEndian: data.load(as: Int16.self))))
+            case 4: self.base = .signed(Int64(Int32(bigEndian: data.load(as: Int32.self))))
+            case 8: self.base = .signed(Int64(bigEndian: data.load(as: Int64.self)))
+            default: self.base = .null
+            }
+            
+        case 0x75:
+            
+            switch data.count {
+            case 1: self.base = .unsigned(UInt64(data.load(as: UInt8.self)))
+            case 2: self.base = .unsigned(UInt64(UInt16(bigEndian: data.load(as: UInt16.self))))
+            case 4: self.base = .unsigned(UInt64(UInt32(bigEndian: data.load(as: UInt32.self))))
+            case 8: self.base = .unsigned(UInt64(bigEndian: data.load(as: UInt64.self)))
+            default: self.base = .null
+            }
+            
+        case 0x6E:
+            
+            switch data.count {
+            case 4: self.base = .number(Double(Float(bitPattern: UInt32(bigEndian: data.load(as: UInt32.self)))))
+            case 8: self.base = .number(Double(bitPattern: UInt64(bigEndian: data.load(as: UInt64.self))))
+            default: self.base = .null
+            }
+            
+        case 0x73:
+            
+            if let string = String(bytes: data, encoding: .utf8) {
+                
+                self.base = .string(string)
+                
+            } else {
+                
+                self.base = .null
+            }
+            
+        case 0x62:
+            
+            self.base = .binary(data)
+            
+        case 0x67:
+            
+            if data.count == 16 {
+                
+                self.base = .uuid(UUID(uuid: data.load(as: uuid_t.self)))
+                
+            } else {
+                
+                self.base = .null
+            }
+            
+        case 0x61:
+            
+            self.base = .undecoded_array(UndecodedArray(data: data, xref: xref, stack: stack))
+            
+        case 0x64:
+            
+            let keyValuePairs = UndecodedKeyValuePairs(data: data, xref: xref, stack: stack)
+            self.base = .dictionary(Dictionary(keyValuePairs.lazy.compactMap({ $0 })) { lhs, _ in lhs })
+            
+        case 0x78:
+            
+            if data.count == 16 {
+                
+                let range = data.load(as: (UInt64, UInt64).self)
+                
+                let startIndex = Int(UInt64(bigEndian: range.0))
+                let endIndex = Int(UInt64(bigEndian: range.1))
+                
+                if !stack.contains(startIndex) {
+                    
+                    var stack = stack
+                    stack.insert(startIndex)
+                    
+                    self = DataPack(decode: xref.dropFirst(startIndex).prefix(endIndex - startIndex), xref: xref, stack: stack)
+                    return
+                    
+                } else {
+                    
+                    self.base = .null
+                }
+                
+            } else {
+                
+                self.base = .null
+            }
+            
+        default: self.base = .null
+        }
+    }
+}
+
+extension DataPack {
+    
+    @inlinable
+    public func encode() -> Data {
+        var result = MappedBuffer<UInt8>()
+        var xref: [DataPack: (Int, Int)] = [:]
+        self.encode(to: &result, base_offset: 0, xref: &xref)
+        return result.data
+    }
+    
+    @inlinable
+    func encode(to data: inout MappedBuffer<UInt8>, base_offset: Int, xref: inout [DataPack: (Int, Int)]) {
+        
+        if let (startIndex, endIndex) = xref[self] {
+            
+            data.append(0x78)
+            data.encode(BEUInt64(startIndex))
+            data.encode(BEUInt64(endIndex))
+            
+            return
+        }
+        
+        let startIndex = data.count + base_offset
+        
+        switch self.base {
+        
         case let .boolean(value):
             
             if value {

        
        let offsets = data.prefix(table_size).typed(as: UInt64.self)
        
        let from = Int(UInt64(bigEndian: offsets[index]))
        let to = Int(UInt64(bigEndian: offsets[index + 1]))
        
        guard to > from && data.count >= table_size + to else { return nil }
        
        return DataPack(decode: data.dropFirst(table_size).dropFirst(from).prefix(to - from))
    }
    
    @inlinable
    subscript(keyValuePairs index: Int) -> (String, DataPack)? {
        
        guard type == .dictionary else { return nil }
        
        let count = self.count
        let table_size = (count + 1) << 4

        guard data.count > table_size else { return nil }
        
        let offsets = data.prefix(table_size).typed(as: (UInt64, UInt64).self)
        
        let startIndex = Int(UInt64(bigEndian: offsets[index].1))
        let splitIndex = Int(UInt64(bigEndian: offsets[index + 1].0))
        let endIndex = Int(UInt64(bigEndian: offsets[index + 1].1))
        
        guard splitIndex > startIndex && endIndex > splitIndex && data.count >= table_size + endIndex else { return nil }
        
        let _key = data.dropFirst(table_size).dropFirst(startIndex).prefix(splitIndex - startIndex)
        let _value = data.dropFirst(table_size).dropFirst(splitIndex).prefix(endIndex - splitIndex)
        
        guard let key = String(bytes: _key, encoding: .utf8) else { return nil }
        
        let value = DataPack(decode: _value, xref: xref, stack: stack)
        return value.isNil ? nil : (key, value)
    }
}

extension DataPack {
    
    @inlinable
    public var count: Int {
        switch self.base {
        case let .array(value): return value.count
        case let .undecoded_array(value): return value.count
        case let .dictionary(value): return value.count
        default: fatalError("Not an array or object.")
        }
