Writing stats to file is now a function