Fix the stats constuction
Use tail-recursive functions