After reading through this block further, I think this could be condensed and much DRY-er. I started trying to refactor it in here, but would be happy to think through it tomorrow. Just pulling out things like assigning total_X = 0 multiple times, val[stat_name] = total, etc.