Moved counter assignment out of the conditional statements