Further bookkeeping fixes: make sure to set the initial count to a reasonable value (oops) and don't double-count connection closure (double-oops).
Moved counter management into dedicated methods for clarity. Fixed another double-counting problem.