Fix handle of end of stream and add bunch of logging