Added code to write out statistics to a file.