fixed counting and a few other bugs.