trying to fix closed stream problem