more optimizations to stats code