     }
 
     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
        }

            }
        }
        
        if data.length > offset {
            data.getBytes(&buffer, range: NSMakeRange(offset, 1))
            if (buffer[0] == BertType.Nil.rawValue) {
                offset++
            }
        }
        
        return BertList(fromElements: elements)
    }
    
    class func decodeNil(data: NSData, inout offset: Int) -> BertObject {
        offset++
        return BertList(fromElements: [])
    }
}