improved event counting algorithm