Improved counter algorithm.