diff --git a/Makefile.am b/Makefile.am
index e546a15044412c94342d7939ec7a900567613b2d..1395020c877840a9798e85c738a164c003ecf277 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,6 +27,12 @@ endif
 
 TESTSUITE_BIN = $(top_srcdir)/src/testsuite/testsuite
 
+if TESTSUITE_VALGRIND
+TEST_BIN = valgrind -q --error-exitcode=1 --leak-check=full $(TESTSUITE_BIN) 
+else
+TEST_BIN = $(TESTSUITE_BIN)
+endif
+
 test_cases = \
 	tests/testsuite.svtest\
 	tests/control-structures.svtest \
@@ -52,8 +58,8 @@ test_cases = \
 	tests/extensions/variables/string.svtest \
 	tests/extensions/variables/errors.svtest \
 	tests/extensions/variables/regex.svtest \
-	tests/extensions/include/variables.svtest \
 	tests/extensions/include/errors.svtest \
+	tests/extensions/include/variables.svtest \
 	tests/extensions/imapflags/basic.svtest \
 	tests/extensions/imapflags/hasflag.svtest \
 	tests/extensions/imapflags/execute.svtest \
@@ -74,7 +80,7 @@ test_cases = \
 if HAVE_DOVECOT_LIBS
 
 $(test_cases):
-	@$(TESTSUITE_BIN) $@
+	@$(TEST_BIN) $@
 
 .PHONY: $(test_cases)
 
diff --git a/configure.in b/configure.in
index 5e39a346ff7432ecf28144b8e55337ea939d0aee..d84b6750796ee3372b0c559fd866099ab555bc43 100644
--- a/configure.in
+++ b/configure.in
@@ -69,6 +69,16 @@ fi
 #
 #
 
+AC_ARG_ENABLE(valgrind,
+[AC_HELP_STRING([--enable-valgrind], [Enable Valgrind memory leak checks in testsuite [default=no]])],
+    if test x$enableval = xno || test x$enableval = xauto; then
+        want_valgrind=$enableval
+    else
+        want_valgrind=yes
+    fi,
+    want_valgrind=no)
+AM_CONDITIONAL(TESTSUITE_VALGRIND, test "$want_valgrind" = "yes")
+
 dnl replace relative ../ paths in the file with full paths
 eval `cat $dovecotdir/dovecot-config|sed 's,\$(top_builddir)/,$dovecotdir/,g'`
 
diff --git a/src/lib-sieve/plugins/include/ext-include-binary.c b/src/lib-sieve/plugins/include/ext-include-binary.c
index fc5525ef44d36ab882fc373cd3e73d7107ed587d..a722e20e12ed83b5e98e69949a4857e510454719 100644
--- a/src/lib-sieve/plugins/include/ext-include-binary.c
+++ b/src/lib-sieve/plugins/include/ext-include-binary.c
@@ -68,7 +68,9 @@ static struct ext_include_binary_context *ext_include_binary_create_context
 		(hash_callback_t *) sieve_script_hash, 
 		(hash_cmp_callback_t *) sieve_script_cmp);
 	p_array_init(&ctx->include_index, pool, 128);
-	
+
+	sieve_binary_extension_set(sbin, &include_binary_ext, ctx);
+
 	return ctx;
 }
 
@@ -78,10 +80,8 @@ struct ext_include_binary_context *ext_include_binary_get_context
 	struct ext_include_binary_context *ctx = (struct ext_include_binary_context *)
 		sieve_binary_extension_get_context(sbin, &include_extension);
 	
-	if ( ctx == NULL ) {
+	if ( ctx == NULL )
 		ctx = ext_include_binary_create_context(sbin);
-		sieve_binary_extension_set_context(sbin, &include_extension, ctx);
-	};
 	
 	return ctx;
 }
@@ -344,7 +344,6 @@ static void ext_include_binary_free(struct sieve_binary *sbin)
 
 	if ( binctx->global_vars != NULL ) 
 		sieve_variable_scope_unref(&binctx->global_vars);
-
 }
 
 /*
diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c
index 621e3691873017d18935102dca6333d61198d36e..63cee40fa39bbd223b1cdd864ddfecdd211c41c1 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.c
+++ b/src/lib-sieve/plugins/include/ext-include-common.c
@@ -586,9 +586,10 @@ bool ext_include_execute_include
 		/* Free any sub-interpreters that might still be active */
 		while ( curctx != NULL && curctx->parent != NULL ) {
 			struct ext_include_interpreter_context *nextctx	= curctx->parent;
+			struct sieve_interpreter *killed_interp = curctx->interp;
 
 			/* This kills curctx too */
-			sieve_interpreter_free(&curctx->interp);
+			sieve_interpreter_free(&killed_interp);
 
 			/* Luckily we recorded the parent earlier */
 			curctx = nextctx;
diff --git a/src/lib-sieve/plugins/include/ext-include.c b/src/lib-sieve/plugins/include/ext-include.c
index 58f3f63ad71f0fe413bdc341f981d28bfe4e456b..20c4b7213ee8db0b276544e8c49564cf020d2296 100644
--- a/src/lib-sieve/plugins/include/ext-include.c
+++ b/src/lib-sieve/plugins/include/ext-include.c
@@ -50,7 +50,6 @@ static const struct sieve_operation *ext_include_operations[] = {
 static bool ext_include_load(int ext_id);
 static bool ext_include_validator_load(struct sieve_validator *validator);
 static bool ext_include_generator_load(const struct sieve_codegen_env *cgenv);
-static bool ext_include_binary_load(struct sieve_binary *sbin);
 static bool ext_include_interpreter_load
 	(const struct sieve_runtime_env *renv, sieve_size_t *address);
 
@@ -65,7 +64,7 @@ const struct sieve_extension include_extension = {
 	ext_include_validator_load, 
 	ext_include_generator_load,
 	ext_include_interpreter_load,
-	ext_include_binary_load, 
+	NULL,
 	ext_include_binary_dump,
 	ext_include_code_dump,
 	SIEVE_EXT_DEFINE_OPERATIONS(ext_include_operations),
@@ -107,13 +106,3 @@ static bool ext_include_interpreter_load
 	return TRUE;
 }
 
-static bool ext_include_binary_load(struct sieve_binary *sbin)
-{
-	/* Register extension to the binary object to get notified of events like 
-	 * opening or saving the binary. The implemententation of these hooks is found
-	 * in ext-include-binary.c
-	 */
-	sieve_binary_extension_set(sbin, &include_extension, &include_binary_ext);
-	
-	return TRUE;
-}
diff --git a/src/lib-sieve/sieve-binary.c b/src/lib-sieve/sieve-binary.c
index 3c79a089cb11262310aa0c577dc34678cdc61b95..133f4a89968505e30e998f553b5a66b8f4336765 100644
--- a/src/lib-sieve/sieve-binary.c
+++ b/src/lib-sieve/sieve-binary.c
@@ -1289,13 +1289,16 @@ const void *sieve_binary_extension_get_context
 }
 
 void sieve_binary_extension_set
-(struct sieve_binary *sbin, const struct sieve_extension *ext, 
-	const struct sieve_binary_extension *bext)
+(struct sieve_binary *sbin, const struct sieve_binary_extension *bext,
+	void *context)
 {
 	struct sieve_binary_extension_reg *ereg = 
-		sieve_binary_extension_get_reg(sbin, ext, TRUE);
+		sieve_binary_extension_get_reg(sbin, bext->extension, TRUE);
 	
 	ereg->binext = bext;
+
+	if ( context != NULL )
+		ereg->context = context;
 }
 
 unsigned int sieve_binary_extension_create_block
diff --git a/src/lib-sieve/sieve-binary.h b/src/lib-sieve/sieve-binary.h
index d934c86cabd40dd4bf59cf9e8e4ed862fd265b66..edb0e0f17da68f11f02db3d15888f722e7cef450 100644
--- a/src/lib-sieve/sieve-binary.h
+++ b/src/lib-sieve/sieve-binary.h
@@ -83,8 +83,8 @@ const void *sieve_binary_extension_get_context
 	(struct sieve_binary *sbin, const struct sieve_extension *ext);
 	
 void sieve_binary_extension_set
-	(struct sieve_binary *sbin, const struct sieve_extension *ext, 
-		const struct sieve_binary_extension *bext);
+	(struct sieve_binary *sbin, const struct sieve_binary_extension *bext,
+		void *context);
 
 unsigned int sieve_binary_extension_create_block
 	(struct sieve_binary *sbin, const struct sieve_extension *ext);