Make sure file handles are closed in case exceptions occur