rewritten counting engine