Refactor how statistics are written into several files