Making sure that all streams are closed properly in case of an exception