Fixed writing to stream