Corrected read() behaviour

Read position now resets after the end of the stream is reached.
Re-formatted code