debug code
fixed up some bugs in counting