Guarantee output files are closed at all times, cleanup code.