Fixed stream not being properly ended when mode reversion occurs