shameful rewrite of stats code