fixed counter bug and added count check