     }
 
     class func decode (data: NSData) throws -> BertObject {
+        if data.length == 0 {
+            return BertUndefined()
+        }
+        
         var offset = 0
         var buffer = [UInt8](count: 1, repeatedValue: 0)
 

            case BertType.SmallTuple.rawValue:   return try decodeTuple(data, offset: &offset)
            case BertType.LargeTuple.rawValue:   return try decodeTuple(data, offset: &offset)
            case BertType.List.rawValue:         return try decodeList(data, offset: &offset)
        default:
            throw BertError.UnexpectedErlangType
        }


        data.getBytes(&buffer, range: NSMakeRange(offset, 1))
        let type = buffer[0]

        switch type {
            case BertType.Atom.rawValue:         return try decodeAtom(data, offset: &offset)
            case BertType.SmallAtom.rawValue:    return try decodeAtom(data, offset: &offset)
