Refix last bug, fixed bug in counting function