Improved the word count algorithm