Fix bug in counting