Write most of export_stats command