Trying to fix "closed stream (IOError)"