merged count method from @crino 's pull request