refactor and add count() method