refactored countwordsintext method