handle end of stream better