Enhanced code and use of memorystream.