Cleanup and output file creation.