 	- Throws: `BSONSerializationError` in case of error.
 	- Returns: The serialized BSON data.
 	*/
-	class func BSONObject(stream: InputStream, options opt: BSONReadingOptions) throws -> [String: AnyObject] {
-		precondition(sizeof(Double.self) == 8, "I currently need Double to be 64 bits")
+	class func BSONObject(stream: InputStream, options opt: BSONReadingOptions) throws -> [String: Any] {
+		precondition(MemoryLayout<Double>.size == 8, "I currently need Double to be 64 bits")
 		
 		var bufferSize = 0
 		var posInBuffer = 0
 		var totalBytesRead = 0
 		let maxBufferSize = 1024*1024 /* 1MB */
-		let buffer = UnsafeMutablePointer<UInt8>(allocatingCapacity: maxBufferSize)
+		let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: maxBufferSize)
 //		if buffer == nil {throw BSONSerializationError.cannotAllocateMemory(maxBufferSize)}
-		defer {buffer.deallocateCapacity(maxBufferSize)}
+		defer {buffer.deallocate(capacity: maxBufferSize)}
 		
 		/* TODO: Handle endianness! */
 		

	- Returns: The required type.
	*/
	private class func readType<Type>(stream: InputStream, buffer: UnsafeMutablePointer<UInt8>, maxBufferSize: Int, totalNReadBytes: inout Int) throws -> Type {
		let size = sizeof(Type.self)
		if maxBufferSize < size {
			/* If the given buffer is too small, we create our own buffer. */
			print("Got too small buffer of size \(maxBufferSize) to read type \(Type.self) of size \(size). Retrying with a bigger buffer.")
			let buffer = UnsafeMutablePointer<UInt8>(allocatingCapacity: size)
//			if buffer == nil {throw BSONSerializationError.cannotAllocateMemory(size)}
			defer {buffer.deallocateCapacity(size)}
			return try readType(stream: stream, buffer: buffer, maxBufferSize: size, totalNReadBytes: &totalNReadBytes)
		}
		

			/* The buffer total size is enough to hold the size we want to read.
			 * However, we must relocate data in the buffer so the buffer start
			 * position is 0. */
			buffer.assign(from: bufferStart, count: bufferValidLength); bufferStartPos = 0
			return try readDataInBigEnoughBuffer(
				dataSize: size,
				bufferHandling: (alwaysCopyBytes ? .copyBytes : .useBufferLeaveOwnership),
