change in last commit did not work. was not setting file since the reference got removed before writing. also closing the streams, just for correctness