Fixed bug in reading/writing streams