diff --git a/src/AlinkPostRenderer.php b/src/AlinkPostRenderer.php
index 78833d7..35f00d5 100644
--- a/src/AlinkPostRenderer.php
+++ b/src/AlinkPostRenderer.php
@@ -7,6 +7,7 @@ use Drupal\Component\Utility\Html;
 use Drupal\Core\Url;
 use Drupal\taxonomy\Entity\Term;
 use Wamania\Snowball\English;
+use PHPTrie\Trie;
 
 /**
  * Class AlinkPostRenderer.
@@ -116,36 +117,99 @@ class AlinkPostRenderer {
   public function replace() {
     $dom = Html::load($this->content);
     $xpath = new \DOMXPath($dom);
-
-    $this->existingLinks = $this->extractExistingLinks($xpath);
-    $this->keywords = array_filter($this->getKeywords(), function (Keyword $word) {
-      return !isset($this->existingLinks[$word->getUrl()]);
-    });
-
+    $trie = new Trie();
+    $paragraph_count = 1;
+    $keysfound = array();
+    $this->keywords = $this->getKeywords();
     foreach ($xpath->query($this->xpathSelector) as $node) {
+      $text_global[$paragraph_count] = $node;
       $text = $node->wholeText;
-      $replace = FALSE;
-      if (empty(trim($text))) {
-        continue;
+      $string = htmlentities($text, null, 'utf-8');
+      $content = str_replace("&nbsp;", " ", $string);
+      $text = html_entity_decode($content);
+      $hashop = explode(' ',$text);
+      foreach($hashop as $wordd) {
+        $wordd1 = preg_replace("/[^a-zA-Z]/","",$wordd);
+        $found = $trie->search($wordd1,1);
+        if($found) {
+          $found->add($paragraph_count);
+          $trie->add($wordd1,$found);
+        }
+        else {
+          $set = new \Ds\Set();
+          $set->add($paragraph_count);
+          $trie->add($wordd1,$set);
+        }
+        
       }
-      foreach ($this->keywords as $key => $word) {
-
-        // @TODO: Make it configurable replaceAll vs. replaceFirst
-        $text = $this->replaceFirst($word, '<a href="' . $word->getUrl() . '">' . $word->getText() . '</a>', $text, $count);
-        if ($count) {
-          $replace = TRUE;
-          $this->addExistingLink($word);
+      $paragraph_count = $paragraph_count + 1;
+    }
+    foreach ($this->keywords as $key => $word) {
+      $flaged = 0;
+      $first_flag = 0;
+      $var= explode(' ',$word->getText());
+      foreach($var as $indiword){
+        $set = $trie->search($indiword);
+        if(!$set) {
+          if($indiword == "&") {
+            continue;
+          }
+          $flaged = 1;
           break;
         }
+        if($first_flag > 0) {
+          $temp = $temp->intersect($set);
+        }
+        else{
+          $temp = $set;
+        }
+        $first_flag = $first_flag + 1;
       }
-      if ($replace) {
-        $this->replaceNodeContent($node, $text);
+      if($flaged==0) {
+        if(!$temp->isEmpty()) {
+          foreach($temp as $location_count){
+            $indivisualArray = $keysfound[$location_count];
+            if(!$indivisualArray){
+              $indivisualArray = array();
+            }
+            $key=array();
+            $key[$word->getText()]=$word;
+            $indivisualArray = array_merge($indivisualArray,$key);
+            $keysfound[$location_count] = $indivisualArray;
+            }
+          }
+        } 
+      }
+    $keys=array_keys($keysfound);
+    foreach($keys as $key) {
+      $keywords = $keysfound[$key];
+      $node = $text_global[$key];
+      $text = $node->wholeText;
+      $string = htmlentities($text, null, 'utf-8');
+      $content = str_replace("&nbsp;", " ", $string);
+      $text = html_entity_decode($content);
+      $replace = 0;
+      $key_hash = array();
+      foreach($keywords as $key=>$word) {
+              $time_stamp = microtime();
+              $key_hash['key'. $time_stamp. 'key'] = $word->getUrl();
+              $key_hash['key1'. $time_stamp. 'key1'] = $word->getText();
+              $text = $this->replaceFirst($word, '<a href="key' . $time_stamp. 'key">' . 'key1'. $time_stamp .'key1'. '</a>', $text, $count);
+              if($count){
+                $replace = 1;
+              }
+      }
+      if($replace) {
+        foreach($key_hash as $key=>$value) {
+          $text=str_replace($key,$value,$text);
+        }
+        $this->replaceNodeContent($node, $text);  
       }
     }
-
     return Html::serialize($dom);
   }
 
+
   /**
    * Process the node list to replace links.
    */
@@ -324,6 +388,7 @@ class AlinkPostRenderer {
       $node->parentNode->insertBefore($replacement_node, $node);
     }
     $node->parentNode->removeChild($node);
+    return $node;
   }
 
 }
