diff --git a/src/lib-sieve/sieve-ast.c b/src/lib-sieve/sieve-ast.c
index cba9f2363e0d0340c85cbad0f8c8fe1b3e632353..81609edb67e519f6b6ea181c7251bf10ec01238b 100644
--- a/src/lib-sieve/sieve-ast.c
+++ b/src/lib-sieve/sieve-ast.c
@@ -354,13 +354,14 @@ struct sieve_ast_node *sieve_ast_command_create
 }
 
 /* The AST */
-struct sieve_ast *sieve_ast_create( void ) {
+struct sieve_ast *sieve_ast_create(const char *scriptname) {
 	pool_t pool;
 	struct sieve_ast *ast;
 	
 	pool = pool_alloconly_create("sieve_ast", 4096);	
 	ast = p_new(pool, struct sieve_ast, 1);
 	ast->pool = pool;
+	ast->scriptname = p_strdup(pool, scriptname);
 	
 	ast->root = sieve_ast_node_create(ast, NULL, SAT_ROOT, 0);
 	
diff --git a/src/lib-sieve/sieve-ast.h b/src/lib-sieve/sieve-ast.h
index b7b45e55425475d826c0ebb23813490512f4c78a..0a8f024f198df7f8e0c0337f786dcbc9da2fcc1f 100644
--- a/src/lib-sieve/sieve-ast.h
+++ b/src/lib-sieve/sieve-ast.h
@@ -154,6 +154,8 @@ struct sieve_ast_node {
 struct sieve_ast {
 	pool_t pool;
 	
+	const char *scriptname;
+	
 	struct sieve_ast_node *root;
 };
 	
@@ -191,7 +193,7 @@ struct sieve_ast_node *sieve_ast_command_create
 	(struct sieve_ast_node *parent, const char *identifier, unsigned int source_line);
 	
 /* sieve_ast */
-struct sieve_ast *sieve_ast_create( void );
+struct sieve_ast *sieve_ast_create(const char *scriptname);
 void sieve_ast_ref(struct sieve_ast *ast);
 void sieve_ast_unref(struct sieve_ast **ast);
 
@@ -211,6 +213,7 @@ void sieve_ast_unparse(struct sieve_ast *ast);
 
 /* AST macros */
 #define sieve_ast_root(ast) (ast->root)
+#define sieve_ast_scriptname(ast) (ast->scriptname)
 #define sieve_ast_pool(ast) (ast->pool)
 
 /* AST node macros */
diff --git a/src/lib-sieve/sieve-error.c b/src/lib-sieve/sieve-error.c
index d046b8093b2d8be4f4bb2e94fbe12247007e786f..d56ae4ca3b821ce23e2db2d542456371aa714412 100644
--- a/src/lib-sieve/sieve-error.c
+++ b/src/lib-sieve/sieve-error.c
@@ -4,74 +4,93 @@
 
 #include "sieve-error.h"
 
-/* FIMXE: This error handling is just a stub for what it should be. 
+/* This should be moved to a sieve-errors-private.h when the need for other 
+ * types of (externally defined) error handlers arises.
  */
-
 struct sieve_error_handler {	
+	pool_t pool;
+	
 	int errors;
 	int warnings;
+	
+	void (*verror)
+		(struct sieve_error_handler *ehandler, const char *location, 
+			const char *fmt, va_list args);
+	void (*vwarning)
+		(struct sieve_error_handler *ehandler, const char *location, 
+			const char *fmt, va_list args);
 };
 
-static struct sieve_error_handler default_error_handler;
-
-struct sieve_error_handler *sieve_error_handler_create( void ) {
-	default_error_handler.errors = 0;	
-	default_error_handler.warnings = 0;
-	
-	return &default_error_handler;	
+void sieve_verror
+	(struct sieve_error_handler *ehandler, const char *location, 
+		const char *fmt, va_list args)
+{
+	ehandler->verror(ehandler, location, fmt, args);
+	ehandler->errors++;
 }
 
-void sieve_error(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, ...) 
+void sieve_vwarning
+	(struct sieve_error_handler *ehandler, const char *location, 
+		const char *fmt, va_list args)
 {
-	va_list args;
-	va_start(args, fmt);
-	
-	sieve_verror(ehandler, line, fmt, args);
-	
-	va_end(args);
+	ehandler->vwarning(ehandler, location, fmt, args);
+	ehandler->warnings++;
+}
+
+unsigned int sieve_get_errors(struct sieve_error_handler *ehandler) {
+	return ehandler->errors;
+}
+
+unsigned int sieve_get_warnings(struct sieve_error_handler *ehandler) {
+	return ehandler->errors;
 }
 
-void sieve_warning(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, ...) 
+void sieve_error_handler_free(struct sieve_error_handler **ehandler)
 {
-	va_list args;
-	va_start(args, fmt);
+	pool_t pool;
 	
-	sieve_vwarning(ehandler, line, fmt, args);
+	if ( *ehandler != NULL ) {
+		pool = (*ehandler)->pool;
+		pool_unref(&pool);
 	
-	va_end(args);
+		if ( pool == NULL )
+			*ehandler = NULL;
+	}
 }
 
-void sieve_verror(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, va_list args) 
-{
-	const char *nfmt;
-				
-	/* FIXME: This seems cumbersome.. */
-	t_push();
-	nfmt = t_strdup_printf("%d: error: %s.\n", line, fmt);
-	vprintf(nfmt, args);
- 	t_pop();
-  
-	ehandler->errors++;
-}
+/* Output errors directly to stderror */
 
-void sieve_vwarning(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, va_list args) 
+static void sieve_stderr_verror
+(struct sieve_error_handler *ehandler ATTR_UNUSED, const char *location, 
+	const char *fmt, va_list args) 
 {
-	const char *nfmt;
-				
-	/* FIXME: This seems cumbersome.. */
-	t_push();
-	nfmt = t_strdup_printf("%d: warning: %s.\n", line, fmt);
-	vprintf(nfmt, args);
-	t_pop();
-  
-	ehandler->warnings++;
+	fprintf(stderr, "%s: error: %s.\n", location, t_strdup_vprintf(fmt, args));
 }
 
-unsigned int sieve_get_errors(struct sieve_error_handler *ehandler) {
-	return ehandler->errors;
+static void sieve_stderr_vwarning
+(struct sieve_error_handler *ehandler ATTR_UNUSED, const char *location, 
+	const char *fmt, va_list args) 
+{
+	fprintf(stderr, "%s: warning: %s.\n", location, t_strdup_vprintf(fmt, args));
 }
 
-unsigned int sieve_get_warnings(struct sieve_error_handler *ehandler) {
-	return ehandler->errors;
+struct sieve_error_handler *sieve_stderr_ehandler_create( void ) 
+{
+	pool_t pool;
+	struct sieve_error_handler *ehandler;
+	
+	/* Pool is not strictly necessary, but other hander types will need a pool,
+	 * so this one will have one too.
+	 */
+	pool = pool_alloconly_create
+		("stderr_error_handler", sizeof(struct sieve_error_handler));	
+	ehandler = p_new(pool, struct sieve_error_handler, 1);
+	ehandler->pool = pool;
+	ehandler->errors = 0;
+	ehandler->warnings = 0;
+	ehandler->verror = sieve_stderr_verror;
+	ehandler->vwarning = sieve_stderr_vwarning;
+	
+	return ehandler;	
 }
 
diff --git a/src/lib-sieve/sieve-error.h b/src/lib-sieve/sieve-error.h
index 0430f34482beaf8523e559215cb646c2360eb0f3..0cf2ef7d8209dbe9021991f4f6eda0e6d186b17b 100644
--- a/src/lib-sieve/sieve-error.h
+++ b/src/lib-sieve/sieve-error.h
@@ -5,17 +5,44 @@
 
 struct sieve_error_handler;
 
-struct sieve_error_handler *sieve_error_handler_create( void );
-
-void sieve_error(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, ...);
-void sieve_warning(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, ...);
-
-void sieve_verror(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, va_list args);
-void sieve_vwarning(struct sieve_error_handler *ehandler, unsigned int line, const char *fmt, va_list args); 
+void sieve_verror
+	(struct sieve_error_handler *ehandler, const char *location, 
+		const char *fmt, va_list args);
+void sieve_vwarning
+	(struct sieve_error_handler *ehandler, const char *location, 
+		const char *fmt, va_list args); 
+
+inline static void sieve_error
+(struct sieve_error_handler *ehandler, const char *location, 
+	const char *fmt, ...) 
+{
+	va_list args;
+	va_start(args, fmt);
+	
+	sieve_verror(ehandler, location, fmt, args);
+	
+	va_end(args);
+}
+
+inline static void sieve_warning
+(struct sieve_error_handler *ehandler, const char *location, 
+	const char *fmt, ...) 
+{
+	va_list args;
+	va_start(args, fmt);
+	
+	sieve_vwarning(ehandler, location, fmt, args);
+	
+	va_end(args);
+}
 
 unsigned int sieve_get_errors(struct sieve_error_handler *ehandler);
 unsigned int sieve_get_warnings(struct sieve_error_handler *ehandler);
 
-struct sieve_error_handler *sieve_error_handler_create();
+void sieve_error_handler_free(struct sieve_error_handler **ehandler);
+
+/* STDERR handler */
+
+struct sieve_error_handler *sieve_stderr_ehandler_create(void); 
 
 #endif /* __SIEVE_ERROR_H */
diff --git a/src/lib-sieve/sieve-lexer.c b/src/lib-sieve/sieve-lexer.c
index 7f34918e4f36767bba67fa390f1c41e63cc81d85..33db7b996a96d6473035a012c2e9f176fc2b3e32 100644
--- a/src/lib-sieve/sieve-lexer.c
+++ b/src/lib-sieve/sieve-lexer.c
@@ -17,11 +17,9 @@
 #define IS_ALPHA(c) ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
 #define IS_QUANTIFIER(c) (c == 'K' || c == 'M' || c =='G') 
 
-#define sieve_lexer_error(lexer, ...) sieve_error(lexer->ehandler, lexer->current_line, __VA_ARGS__)
-#define sieve_lexer_warning(lexer, ...) sieve_warning(lexer->ehandler, lexer->current_line, __VA_ARGS__)
-
 struct sieve_lexer {
 	pool_t pool;
+	const char *scriptname;
 	struct istream *input;
 		
 	int current_line;
@@ -38,11 +36,41 @@ struct sieve_lexer {
 	size_t buffer_pos;
 };
 
-struct sieve_lexer *sieve_lexer_create(struct istream *stream, struct sieve_error_handler *ehandler) {
+inline static void sieve_lexer_error
+(struct sieve_lexer *lexer, const char *fmt, ...) 
+{
+	va_list args;
+	va_start(args, fmt);
+
+	sieve_verror(lexer->ehandler, 
+		t_strdup_printf("%s:%d", lexer->scriptname, lexer->current_line),
+		fmt, args);
+		
+	va_end(args);
+}
+
+inline static void sieve_lexer_warning
+(struct sieve_lexer *lexer, const char *fmt, ...) 
+{
+	va_list args;
+	va_start(args, fmt);
+
+	sieve_vwarning(lexer->ehandler, 
+		t_strdup_printf("%s:%d", lexer->scriptname, lexer->current_line),
+		fmt, args);
+		
+	va_end(args);
+}
+
+struct sieve_lexer *sieve_lexer_create
+(struct istream *stream, const char *scriptname, 
+	struct sieve_error_handler *ehandler) 
+{
 	pool_t pool = pool_alloconly_create("sieve_lexer", 1024);	
 	struct sieve_lexer *lexer = p_new(pool, struct sieve_lexer, 1);
 
 	lexer->pool = pool;
+	lexer->scriptname = p_strdup(pool, scriptname);
 	lexer->input = stream;
 	
 	lexer->buffer = NULL;
diff --git a/src/lib-sieve/sieve-lexer.h b/src/lib-sieve/sieve-lexer.h
index c2f56c3522d0a61db189a3e5115288faa0f0a728..aa092d6d7d6e053f3bdf207d89be9d305dcdcc4a 100644
--- a/src/lib-sieve/sieve-lexer.h
+++ b/src/lib-sieve/sieve-lexer.h
@@ -40,7 +40,9 @@ enum sieve_token_type {
 struct sieve_token;
 struct sieve_lexer;
 
-struct sieve_lexer *sieve_lexer_create(struct istream *stream, struct sieve_error_handler *ehandler);
+struct sieve_lexer *sieve_lexer_create
+	(struct istream *stream, const char *scriptname,  
+		struct sieve_error_handler *ehandler);
 void sieve_lexer_free(struct sieve_lexer *lexer);
 
 bool sieve_lexer_scan_raw_token(struct sieve_lexer *lexer);
diff --git a/src/lib-sieve/sieve-parser.c b/src/lib-sieve/sieve-parser.c
index 5b0638ad49defeb11819f28ef411b6e6b7b09ceb..e8dabd02904f28f7c4e1f0e1ed5c247f99dafc04 100644
--- a/src/lib-sieve/sieve-parser.c
+++ b/src/lib-sieve/sieve-parser.c
@@ -9,23 +9,14 @@
 #include "sieve-error.h"
 #include "sieve-ast.h"
 
-/* FIXME: Enforce maximums on the number of arguments, tests, commands, nesting levels, etc.
- *
+/* FIXME: Enforce maximums on the number of arguments, tests, commands, 
+ * nesting levels, etc.
  */
 
-#define SIEVE_READ_BLOCK_SIZE (1024*8)
-
-#define sieve_parser_error(parser, ...) \
-	{ \
-		/* Don't report a parse error if the lexer complained already */ \
-		if ( sieve_lexer_current_token(parser->lexer) != STT_ERROR )  \
-			sieve_error(parser->ehandler, sieve_lexer_current_line(parser->lexer), __VA_ARGS__); \
-	}
-#define sieve_parser_warning(parser, ...) \
-	sieve_error(parser->ehandler, sieve_lexer_current_line(parser->lexer), __VA_ARGS__)
-
 struct sieve_parser {
 	pool_t pool;
+	const char *scriptname;
+	
 	struct istream *input;
 	
 	struct sieve_error_handler *ehandler;
@@ -34,10 +25,46 @@ struct sieve_parser {
 	struct sieve_ast *ast;
 };
 
+#define SIEVE_READ_BLOCK_SIZE (1024*8)
+
+inline static void sieve_parser_error
+	(struct sieve_parser *parser, const char *fmt, ...) 
+{ 
+	va_list args;
+	va_start(args, fmt);
+
+	/* Don't report a parse error if the lexer complained already */ 
+	if ( sieve_lexer_current_token(parser->lexer) != STT_ERROR )  
+	{
+		sieve_verror(parser->ehandler, 
+			t_strdup_printf("%s:%d", parser->scriptname,
+			sieve_lexer_current_line(parser->lexer)),
+			fmt, args); 
+	}
+	
+	va_end(args);
+}
+
+inline static void sieve_parser_warning
+	(struct sieve_parser *parser, const char *fmt, ...) 
+{
+	va_list args;
+	va_start(args, fmt);
+
+	sieve_vwarning(parser->ehandler, 
+		t_strdup_printf("%s:%d", parser->scriptname,
+		sieve_lexer_current_line(parser->lexer)),
+		fmt, args);
+		
+	va_end(args);
+} 
+
 /* Forward declarations */
-static bool sieve_parser_recover(struct sieve_parser *parser, enum sieve_token_type end_token);
+static bool sieve_parser_recover
+	(struct sieve_parser *parser, enum sieve_token_type end_token);
 
-struct sieve_parser *sieve_parser_create(int fd, struct sieve_ast *ast, struct sieve_error_handler *ehandler) 
+struct sieve_parser *sieve_parser_create
+(int fd, const char *scriptname, struct sieve_error_handler *ehandler)
 {
 	struct sieve_parser *parser;
 	struct istream *stream;
@@ -49,12 +76,12 @@ struct sieve_parser *sieve_parser_create(int fd, struct sieve_ast *ast, struct s
 		pool_t pool = pool_alloconly_create("sieve_parser", 4096);	
 
 		parser = p_new(pool, struct sieve_parser, 1);
-		parser->input = stream;
 		parser->pool = pool;
+		parser->scriptname = p_strdup(pool, scriptname);
+		parser->input = stream;
 		
-		parser->lexer = sieve_lexer_create(stream, ehandler);
-		parser->ast = ast;
-		sieve_ast_ref(ast);
+		parser->lexer = sieve_lexer_create(stream, scriptname, ehandler);
+		parser->ast = NULL;
 		
 		parser->ehandler = ehandler;
 		
@@ -68,8 +95,9 @@ void sieve_parser_free(struct sieve_parser *parser)
 {
 	if (parser->input != NULL ) 
 		i_stream_unref(&parser->input);
-	  
-	sieve_ast_unref(&parser->ast);
+
+	if (parser->ast != NULL)	  
+		sieve_ast_unref(&parser->ast);
 
 	sieve_lexer_free(parser->lexer);
 
@@ -340,22 +368,32 @@ static bool sieve_parse_commands
 	return result;
 }
 
-bool sieve_parser_run(struct sieve_parser *parser) 
+bool sieve_parser_run
+	(struct sieve_parser *parser, struct sieve_ast **ast) 
 {
+	if ( parser->ast != NULL )
+		sieve_ast_unref(&parser->ast);
+	
+	*ast = NULL;
+	parser->ast = sieve_ast_create(parser->scriptname);
+
 	/* Scan first token */
 	sieve_lexer_skip_token(parser->lexer);
 
 	if ( sieve_parse_commands(parser, parser->ast->root) ) { 
 		if ( sieve_lexer_current_token(parser->lexer) != STT_EOF ) { 
-			sieve_parser_error(parser, "unexpected token %s found at (the presumed) end of file",
+			sieve_parser_error(parser, 
+				"unexpected token %s found at (the presumed) end of file",
 				sieve_lexer_token_string(parser->lexer));
-				
+			sieve_ast_unref(&parser->ast);
 			return FALSE;				
 		}
 		
+		*ast = parser->ast;
 		return TRUE;
 	} 
 	
+	sieve_ast_unref(&parser->ast);
 	return FALSE;
 }	
 
diff --git a/src/lib-sieve/sieve-parser.h b/src/lib-sieve/sieve-parser.h
index ff6901288eb478b2bfd92540c57870f1394241f0..b0c81e28190a2c1c064b2f365c91c62739ab3b90 100644
--- a/src/lib-sieve/sieve-parser.h
+++ b/src/lib-sieve/sieve-parser.h
@@ -7,8 +7,9 @@
 
 struct sieve_parser;
 
-struct sieve_parser *sieve_parser_create(int fd, struct sieve_ast *ast, struct sieve_error_handler *ehandler);
+struct sieve_parser *sieve_parser_create
+	(int fd, const char *scriptname, struct sieve_error_handler *ehandler);
 void sieve_parser_free(struct sieve_parser *parser);
-bool sieve_parser_run(struct sieve_parser *parser);
+bool sieve_parser_run(struct sieve_parser *parser, struct sieve_ast **ast);
 
 #endif /* __SIEVE_PARSER_H */
diff --git a/src/lib-sieve/sieve-validator.c b/src/lib-sieve/sieve-validator.c
index 8ef052121604ff5a57e3c7887638cd940d97ca7d..aecb05bfe8c30a30339023265a82c5d89da09d3a 100644
--- a/src/lib-sieve/sieve-validator.c
+++ b/src/lib-sieve/sieve-validator.c
@@ -15,6 +15,7 @@
 
 struct sieve_validator {
 	pool_t pool;
+	const char *scriptname;
 	struct sieve_ast *ast;
 	
 	struct sieve_error_handler *ehandler;
@@ -39,7 +40,9 @@ void sieve_validator_warning
 	va_list args;
 	va_start(args, fmt);
 	
-	sieve_vwarning(validator->ehandler, sieve_ast_node_line(node), fmt, args); 
+	sieve_vwarning(validator->ehandler, 
+		t_strdup_printf("%s:%d", sieve_ast_scriptname(validator->ast),
+			sieve_ast_node_line(node)), fmt, args); 
 	
 	va_end(args);
 }
@@ -51,12 +54,15 @@ void sieve_validator_error
 	va_list args;
 	va_start(args, fmt);
 	
-	sieve_verror(validator->ehandler, sieve_ast_node_line(node), fmt, args); 
+	sieve_verror(validator->ehandler, 
+		t_strdup_printf("%s:%d", sieve_ast_scriptname(validator->ast),
+		sieve_ast_node_line(node)), fmt, args); 
 	
 	va_end(args);
 }
 
-struct sieve_validator *sieve_validator_create(struct sieve_ast *ast, struct sieve_error_handler *ehandler) 
+struct sieve_validator *sieve_validator_create
+	(struct sieve_ast *ast, struct sieve_error_handler *ehandler) 
 {
 	unsigned int i;
 	pool_t pool;
diff --git a/src/lib-sieve/sieve.c b/src/lib-sieve/sieve.c
index 01c35b711aa088069afb646230c1dda4f26888a4..51fef4a831ffaad2d5f40ffbac346d9df260e2a9 100644
--- a/src/lib-sieve/sieve.c
+++ b/src/lib-sieve/sieve.c
@@ -36,22 +36,22 @@ void sieve_deinit(void)
 	sieve_extensions_deinit();
 }
 
-static struct sieve_ast *sieve_parse(int fd, struct sieve_error_handler *ehandler)
+static struct sieve_ast *sieve_parse
+	(int fd, const char *scriptname, struct sieve_error_handler *ehandler)
 {
 	struct sieve_parser *parser;
 	struct sieve_ast *ast;
 	
-	/* Construct ast */
-	ast = sieve_ast_create();
-
 	/* Parse */
-	parser = sieve_parser_create(fd, ast, ehandler);
+	parser = sieve_parser_create(fd, scriptname, ehandler);
 
- 	if ( !sieve_parser_run(parser) || sieve_get_errors(ehandler) > 0 ) {
- 		/* Failed */
- 		sieve_ast_unref(&ast); 
- 		ast = NULL; /* Explicit */
+ 	if ( !sieve_parser_run(parser, &ast) || sieve_get_errors(ehandler) > 0 ) {
+ 		if ( ast != NULL )
+ 			/* This really shouldn't happen */
+ 			sieve_ast_unref(&ast);
+ 		ast = NULL;
  	} else 
+		sieve_ast_ref(ast);
 	
 	sieve_parser_free(parser); 	
 	
@@ -83,24 +83,21 @@ static struct sieve_binary *sieve_generate(struct sieve_ast *ast)
 	return result;
 }
 
-static struct sieve_binary *sieve_compile_fd(int fd, const char *name) 
+static struct sieve_binary *sieve_compile_fd
+(int fd, const char *scriptname, struct sieve_error_handler *ehandler) 
 {
 	struct sieve_binary *result;
-	struct sieve_error_handler *ehandler;
 	struct sieve_ast *ast;
-  
-	/* Construct error handler */
-	ehandler = sieve_error_handler_create();  
-	
+  	
 	/* Parse */
-	if ( (ast = sieve_parse(fd, ehandler)) == NULL ) {
- 		i_error("Failed to parse script %s", name);
+	if ( (ast = sieve_parse(fd, scriptname, ehandler)) == NULL ) {
+ 		sieve_error(ehandler, scriptname, "failed to parse script");
 		return NULL;
 	}
 
 	/* Validate */
 	if ( !sieve_validate(ast, ehandler) ) {
-		i_error("Failed to validate script %s", name);
+		sieve_error(ehandler, scriptname, "failed to validate script");
 		
  		sieve_ast_unref(&ast);
  		return NULL;
@@ -108,7 +105,7 @@ static struct sieve_binary *sieve_compile_fd(int fd, const char *name)
  	
 	/* Generate */
 	if ( (result=sieve_generate(ast)) == NULL ) {
-		i_error("Failed to generate script %s", name);
+		sieve_error(ehandler, scriptname, "failed to generate script");
 		
 		sieve_ast_unref(&ast);
 		return NULL;
@@ -120,17 +117,26 @@ static struct sieve_binary *sieve_compile_fd(int fd, const char *name)
 	return result;
 }
 
-struct sieve_binary *sieve_compile(const char *scriptpath)
+struct sieve_binary *sieve_compile
+	(const char *scriptpath, struct sieve_error_handler *ehandler)
 {
 	int sfd;
+	const char *scriptname;
 	struct sieve_binary *sbin;
 	
+	
 	if ( (sfd = open(scriptpath, O_RDONLY)) < 0 ) {
-		i_error("Failed to open sieve script %s: %m", scriptpath);
+		sieve_error(ehandler, scriptpath, "failed to open sieve script: %m");
 		return NULL;
 	}
 	
-	sbin = sieve_compile_fd(sfd, scriptpath);
+	scriptname = strrchr(scriptpath, '/');
+	if ( scriptname == NULL )	
+		scriptname = scriptpath;
+	else
+		scriptname++;
+	
+	sbin = sieve_compile_fd(sfd, scriptname, ehandler);
 		
 	close(sfd);
 	return sbin;
diff --git a/src/lib-sieve/sieve.h b/src/lib-sieve/sieve.h
index 776a52028feacda9efd2b54e6942a9c9cb339351..b66dd33bc3999b3945d0497cce0706e83ef9d17e 100644
--- a/src/lib-sieve/sieve.h
+++ b/src/lib-sieve/sieve.h
@@ -8,6 +8,8 @@
 
 #include <stdio.h>
 
+#include "sieve-error.h"
+
 #define SIEVE_VERSION "0.0.1"
 #define SIEVE_IMPLEMENTATION "Dovecot Sieve " SIEVE_VERSION
 
@@ -45,7 +47,8 @@ struct sieve_mail_environment {
 bool sieve_init(const char *plugins);
 void sieve_deinit(void);
 
-struct sieve_binary *sieve_compile(const char *scriptpath);
+struct sieve_binary *sieve_compile
+	(const char *scriptpath, struct sieve_error_handler *ehandler);
 void sieve_dump(struct sieve_binary *binary, struct ostream *stream);
 bool sieve_test
 	(struct sieve_binary *binary, const struct sieve_message_data *msgdata, 
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index a64956a27db92fa0a11b6d3258814a0fb35030b1..dd1efc58df8eb9bea9a99032ed9b8521fc05eb78 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -81,13 +81,20 @@ static int lda_sieve_run
 	bool debug = ( getenv("DEBUG") != NULL );
 	struct sieve_message_data msgdata;
 	struct sieve_mail_environment mailenv;
+	struct sieve_error_handler *ehandler;
 	struct sieve_binary *sbin;
 
+	ehandler = sieve_stderr_ehandler_create();
+
 	if ( debug )
 		i_info("lda-sieve: Compiling script %s", script_path);
 
-	if ( (sbin=sieve_compile(script_path)) == NULL )
+	if ( (sbin=sieve_compile(script_path, ehandler)) == NULL ) {
+		sieve_error_handler_free(&ehandler);
 		return -1;
+	}
+
+	sieve_error_handler_free(&ehandler);
 
 	/* Collect necessary message data */
 	memset(&msgdata, 0, sizeof(msgdata));
diff --git a/src/sieve-bin/bin-common.c b/src/sieve-bin/bin-common.c
index ff97f03eed606a701f20f0eecde87de5484898db..31415cf49db7b8d186dcfeb803fcbe7ae7140410 100644
--- a/src/sieve-bin/bin-common.c
+++ b/src/sieve-bin/bin-common.c
@@ -68,10 +68,17 @@ const char *bin_get_user(void)
 
 struct sieve_binary *bin_compile_sieve_script(const char *filename)
 {
+	struct sieve_error_handler *ehandler;
 	struct sieve_binary *sbin;
 	
-	if ( (sbin = sieve_compile(filename)) == NULL ) 
+	ehandler = sieve_stderr_ehandler_create();
+
+	if ( (sbin = sieve_compile(filename, ehandler)) == NULL ) {
+		sieve_error_handler_free(&ehandler);
 		i_fatal("Failed to compile sieve script\n");
+	}
+
+	sieve_error_handler_free(&ehandler);
 		
 	return sbin;
 }
diff --git a/src/sieve-bin/sievec.c b/src/sieve-bin/sievec.c
index 1dc8ca374c0bd6227f96b41fa7ed328f117d69f1..f4c7a9b7277caa9a7f18f744a85c73fe05810191 100644
--- a/src/sieve-bin/sievec.c
+++ b/src/sieve-bin/sievec.c
@@ -24,7 +24,7 @@ int main(int argc, char **argv) {
  	}
   
 	sbin = bin_compile_sieve_script(argv[1]);
-	bin_dump_sieve_binary_to(sbin, "frop.sdump");
+	bin_dump_sieve_binary_to(sbin, "-");
 
 	bin_deinit();
 }