diff --git a/filter_sage.sed b/filter_sage.sed
index 676fa19eb8f752e8619ed73d58111cce5a4de118..8184701c8a5215830ea00deffa44690ea3af642e 100755
--- a/filter_sage.sed
+++ b/filter_sage.sed
@@ -2,12 +2,11 @@
 
 0,/^\\begin{sagesilent}/d
 /^\\end{sagesilent}/,/^\\begin{sagesilent}/{
-	s;.*\\sage{\(.*\)}.*;\n# RENDERED TO LATEX: \1 #;p
-	t underline
-	d
-:underline
-	s;^\n;;
+	s;.*\\sage{\(.*\)}.*;# RENDERED TO LATEX: \1 #;
+	T end; h
 	s;\S;#;g
-	s;$;\n;p
+	H; G; a
+	p
+:end
 	d
 }