Fixed stream closing issues