more accurate HTML comment restoration
diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php
index a4d1e35..3875bd5 100644
--- a/system/libraries/Typography.php
+++ b/system/libraries/Typography.php
@@ -180,9 +180,12 @@
 		// restore HTML comments
 		for ($i = 0, $total = count($html_comments); $i < $total; $i++)
 		{
-			$str = preg_replace('#(?:<p>)?{@HC'.$i.'}(?:\s*</p>)?#s', $html_comments[$i], $str);
+			// remove surrounding paragraph tags, but only if there's an opening paragraph tag
+			// otherwise HTML comments at the ends of paragraphs will have the closing tag removed
+			// if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
+			$str = preg_replace('#(?(?=<p>\{@HC'.$i.'\})<p>\{@HC'.$i.'\}(\s*</p>)|\{@HC'.$i.'\})#s', $html_comments[$i], $str);
 		}
-				
+
 		// Final clean up
 		$table = array(