refactor the streaming handling