stats calculation and some fixes