Add closing of files, extra logging.