End of stream not handled correctly