refactor word count logic