diff --git a/src/Text/Sundown/Foreign.hsc b/src/Text/Sundown/Foreign.hsc
--- a/src/Text/Sundown/Foreign.hsc
+++ b/src/Text/Sundown/Foreign.hsc
@@ -1,3 +1,4 @@
+{-# Language EmptyDataDecls #-}
 {-# Language ForeignFunctionInterface #-}
 module Text.Sundown.Foreign
     ( Extensions (..)
diff --git a/src/Text/Sundown/Html/Foreign.hsc b/src/Text/Sundown/Html/Foreign.hsc
--- a/src/Text/Sundown/Html/Foreign.hsc
+++ b/src/Text/Sundown/Html/Foreign.hsc
@@ -1,8 +1,8 @@
+{-# Language EmptyDataDecls #-}
 {-# Language ForeignFunctionInterface #-}
 module Text.Sundown.Html.Foreign
     ( HtmlRenderMode (..)
     , sdhtml_renderer
-    , sdhtml_torenderer
     , sdhtml_smartypants
     ) where
 
@@ -56,9 +56,6 @@
 sdhtml_renderer rndr options mode = sdhtml_renderer' rndr options (toCUInt mode)
 foreign import ccall "html.h sdhtml_renderer"
     sdhtml_renderer' :: Ptr Callbacks -> Ptr HtmlRenderOptions -> CUInt -> IO ()
-
-foreign import ccall "html.h sdhtml_toc_renderer"
-    sdhtml_torenderer :: Ptr Callbacks -> Ptr HtmlRenderOptions -> IO ()
 
 foreign import ccall "html.h sdhtml_smartypants"
     sdhtml_smartypants :: Ptr Buffer -> CString -> CSize -> IO ()
diff --git a/sundown-c/autolink.c b/sundown-c/autolink.c
--- a/sundown-c/autolink.c
+++ b/sundown-c/autolink.c
@@ -49,7 +49,7 @@
 }
 
 static size_t
-autolink_delim(uint8_t *data, size_t link_end, size_t offset, size_t size)
+autolink_delim(uint8_t *data, size_t link_end, size_t max_rewind, size_t size)
 {
 	uint8_t cclose, copen = 0;
 	size_t i;
@@ -163,13 +163,13 @@
 	size_t *rewind_p,
 	struct buf *link,
 	uint8_t *data,
-	size_t offset,
+	size_t max_rewind,
 	size_t size,
 	unsigned int flags)
 {
 	size_t link_end;
 
-	if (offset > 0 && !ispunct(data[-1]) && !isspace(data[-1]))
+	if (max_rewind > 0 && !ispunct(data[-1]) && !isspace(data[-1]))
 		return 0;
 
 	if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0)
@@ -183,7 +183,7 @@
 	while (link_end < size && !isspace(data[link_end]))
 		link_end++;
 
-	link_end = autolink_delim(data, link_end, offset, size);
+	link_end = autolink_delim(data, link_end, max_rewind, size);
 
 	if (link_end == 0)
 		return 0;
@@ -199,14 +199,14 @@
 	size_t *rewind_p,
 	struct buf *link,
 	uint8_t *data,
-	size_t offset,
+	size_t max_rewind,
 	size_t size,
 	unsigned int flags)
 {
 	size_t link_end, rewind;
 	int nb = 0, np = 0;
 
-	for (rewind = 0; rewind < offset; ++rewind) {
+	for (rewind = 0; rewind < max_rewind; ++rewind) {
 		uint8_t c = data[-rewind - 1];
 
 		if (isalnum(c))
@@ -238,7 +238,7 @@
 	if (link_end < 2 || nb != 1 || np == 0)
 		return 0;
 
-	link_end = autolink_delim(data, link_end, offset, size);
+	link_end = autolink_delim(data, link_end, max_rewind, size);
 
 	if (link_end == 0)
 		return 0;
@@ -254,7 +254,7 @@
 	size_t *rewind_p,
 	struct buf *link,
 	uint8_t *data,
-	size_t offset,
+	size_t max_rewind,
 	size_t size,
 	unsigned int flags)
 {
@@ -263,7 +263,7 @@
 	if (size < 4 || data[1] != '/' || data[2] != '/')
 		return 0;
 
-	while (rewind < offset && isalpha(data[-rewind - 1]))
+	while (rewind < max_rewind && isalpha(data[-rewind - 1]))
 		rewind++;
 
 	if (!sd_autolink_issafe(data - rewind, size + rewind))
@@ -283,7 +283,7 @@
 	while (link_end < size && !isspace(data[link_end]))
 		link_end++;
 
-	link_end = autolink_delim(data, link_end, offset, size);
+	link_end = autolink_delim(data, link_end, max_rewind, size);
 
 	if (link_end == 0)
 		return 0;
diff --git a/sundown-c/houdini.h b/sundown-c/houdini.h
--- a/sundown-c/houdini.h
+++ b/sundown-c/houdini.h
@@ -3,6 +3,10 @@
 
 #include "buffer.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifdef HOUDINI_USE_LOCALE
 #	define _isxdigit(c) isxdigit(c)
 #	define _isdigit(c) isdigit(c)
@@ -25,5 +29,9 @@
 extern void houdini_unescape_url(struct buf *ob, const uint8_t *src, size_t size);
 extern void houdini_escape_js(struct buf *ob, const uint8_t *src, size_t size);
 extern void houdini_unescape_js(struct buf *ob, const uint8_t *src, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif
diff --git a/sundown-c/html.c b/sundown-c/html.c
--- a/sundown-c/html.c
+++ b/sundown-c/html.c
@@ -491,6 +491,13 @@
 {
 	struct html_renderopt *options = opaque;
 
+	/* set the level offset if this is the first header
+	 * we're parsing for the document */
+	if (options->toc_data.current_level == 0) {
+		options->toc_data.level_offset = level - 1;
+	}
+	level -= options->toc_data.level_offset;
+
 	if (level > options->toc_data.current_level) {
 		while (level > options->toc_data.current_level) {
 			BUFPUTSL(ob, "<ul>\n<li>\n");
@@ -509,10 +516,18 @@
 
 	bufprintf(ob, "<a href=\"#toc_%d\">", options->toc_data.header_count++);
 	if (text)
-		bufput(ob, text->data, text->size);
+		escape_html(ob, text->data, text->size);
 	BUFPUTSL(ob, "</a>\n");
 }
 
+static int
+toc_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
+{
+	if (content && content->size)
+		bufput(ob, content->data, content->size);
+	return 1;
+}
+
 static void
 toc_finalize(struct buf *ob, void *opaque)
 {
@@ -546,7 +561,7 @@
 		rndr_emphasis,
 		NULL,
 		NULL,
-		NULL,
+		toc_link,
 		NULL,
 		rndr_triple_emphasis,
 		rndr_strikethrough,
diff --git a/sundown-c/html.h b/sundown-c/html.h
--- a/sundown-c/html.h
+++ b/sundown-c/html.h
@@ -21,10 +21,15 @@
 #include "buffer.h"
 #include <stdlib.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 struct html_renderopt {
 	struct {
 		int header_count;
 		int current_level;
+		int level_offset;
 	} toc_data;
 
 	unsigned int flags;
@@ -63,6 +68,10 @@
 
 extern void
 sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif
 
diff --git a/sundown-c/html_smartypants.c b/sundown-c/html_smartypants.c
--- a/sundown-c/html_smartypants.c
+++ b/sundown-c/html_smartypants.c
@@ -22,6 +22,10 @@
 #include <stdio.h>
 #include <ctype.h>
 
+#if defined(_WIN32)
+#define snprintf	_snprintf		
+#endif
+
 struct smartypants_data {
 	int in_squote;
 	int in_dquote;
diff --git a/sundown-c/markdown.c b/sundown-c/markdown.c
--- a/sundown-c/markdown.c
+++ b/sundown-c/markdown.c
@@ -501,7 +501,7 @@
 		if (data[i] == c && !_isspace(data[i - 1])) {
 
 			if (rndr->ext_flags & MKDEXT_NO_INTRA_EMPHASIS) {
-				if (!(i + 1 == size || _isspace(data[i + 1]) || ispunct(data[i + 1])))
+				if (i + 1 < size && isalnum(data[i + 1]))
 					continue;
 			}
 
@@ -595,6 +595,11 @@
 {
 	uint8_t c = data[0];
 	size_t ret;
+
+	if (rndr->ext_flags & MKDEXT_NO_INTRA_EMPHASIS) {
+		if (offset > 0 && !_isspace(data[-1]) && data[-1] != '>')
+			return 0;
+	}
 
 	if (size > 2 && data[1] != c) {
 		/* whitespace cannot follow an opening emphasis;
diff --git a/sundown.cabal b/sundown.cabal
--- a/sundown.cabal
+++ b/sundown.cabal
@@ -1,6 +1,6 @@
 Cabal-version:      >= 1.6
 Name:               sundown
-Version:            0.5
+Version:            0.5.0.1
 Author:             Francesco Mazzoli (f@mazzo.li)
 Maintainer:         Francesco Mazzoli (f@mazzo.li)
 Build-Type:         Simple
@@ -12,7 +12,7 @@
 Homepage:           https://github.com/bitonic/sundown
 Bug-Reports:        https://github.com/bitonic/sundown/issues
 Description:
-    Bindings to GitHub's C markdown library: <https://github.com/tanoku/sundown>.
+    Bindings to GitHub's C markdown library: <https://github.com/vmg/sundown>.
     . 
     The Haskell code is in the public domain. For sundown's license, refer to
     its GitHub page.
@@ -23,9 +23,12 @@
 
 Library
     Hs-Source-Dirs:   src
-    Build-Depends:    base >= 3 && < 5, bytestring, utf8-string, text
+    Build-Depends:    base >= 3 && < 5,
+                      bytestring,
+                      utf8-string >= 0.3,
+                      text
     GHC-Options:      -Wall -O2 -fno-cse
-    Extensions:       ForeignFunctionInterface, EmptyDataDecls
+    Extensions:       EmptyDataDecls, ForeignFunctionInterface
 
     Exposed-Modules:  Text.Sundown,
                       Text.Sundown.Html.ByteString,
