fix invalid counts (including some debugging code)