Improvements to the counter logic