properly close streams