Refactored and memoised the count method