Added the optimization for the algorithm, for some reason the stats update is still buggy