Refactoring how to count prefixes and counts