counting logic