Fix a bug in encoding, truncating output too much
Finish looping when flusing the stream