Close file handles on exit

Fixes a bug which would cause output files to be incomplete.