refactor the streaming handling

- a little bit of refactoring on the streaming handling
- uncomment write test: passes fine on my machine