fixed bug regarding detection of end of stream + added unit tests