--- embed/ad_embed.module	2009-11-22 19:07:53.000000000 -0800
+++ embed/ad_embed.module	2010-06-05 15:59:29.000000000 -0700
@@ -304,14 +304,24 @@ function ad_embed_auto($text, $ad, $coun
     $text = $text . $ad;
   }
 
-  $pos = $paragraph = 0;
+  $pos = $paragraph = $ptag = $char = 0;
   while ($pos !== FALSE) {
-    $pos = strpos($text, "\n", $pos + 1);
+    $ptag = strpos($text, "</p>", $pos + 1);
+    $char = strpos($text, "\n", $pos + 1);
+    if (($char == FALSE && $ptag !== FALSE) || (($ptag !== FALSE) && ($char !== FALSE) && $ptag < $char)){
+      $len = 4;
+      $pos = $ptag;
+    }
+    else {
+      $len = 0;
+      $pos = $char;
+    }
+
     if ($pos) {
       $paragraph++;
       if ($paragraph == $count) {
-        $part1 = substr($text, 0, $pos);
-        $part2 = substr($text, $pos + 1, strlen($text));
+        $part1 = substr($text, 0, $pos + $len);
+        $part2 = substr($text, $pos + $len + 1, strlen($text));
         $text = $part1 . $ad . $part2;
         break;
       }
