rewrite the run stats script using recursion and a tree structure to make it easier to modify in future use