close input/output streams correctly