bugfix in WriteToStream