Index: lib/ssl_bogus.c
===================================================================
--- lib/ssl_bogus.c	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ lib/ssl_bogus.c	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -27,4 +27,8 @@
 
 int ssl_errno;
+
+void ssl_init( void )
+{
+}
 
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
Index: lib/ssl_client.h
===================================================================
--- lib/ssl_client.h	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ lib/ssl_client.h	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -47,4 +47,7 @@
 
 
+/* Perform any global initialization the SSL library might need. */
+G_MODULE_EXPORT void ssl_init( void );
+
 /* Connect to host:port, call the given function when the connection is
    ready to be used for SSL traffic. This is all done asynchronously, no
Index: lib/ssl_gnutls.c
===================================================================
--- lib/ssl_gnutls.c	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ lib/ssl_gnutls.c	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -60,4 +60,11 @@
 static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
 
+
+void ssl_init( void )
+{
+	gnutls_global_init();
+	initialized = TRUE;
+	atexit( gnutls_global_deinit );
+}
 
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
@@ -122,7 +129,5 @@
 	if( !initialized )
 	{
-		gnutls_global_init();
-		initialized = TRUE;
-		atexit( gnutls_global_deinit );
+		ssl_init();
 	}
 	
Index: lib/ssl_nss.c
===================================================================
--- lib/ssl_nss.c	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ lib/ssl_nss.c	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -91,4 +91,12 @@
 
 
+void ssl_init( void )
+{
+	PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
+	NSS_NoDB_Init(NULL);
+	NSS_SetDomesticPolicy();
+	initialized = TRUE;
+}
+
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
 {
@@ -107,7 +115,5 @@
 	if( !initialized )
 	{
-		PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
-		NSS_NoDB_Init(NULL);
-		NSS_SetDomesticPolicy();
+		ssl_init();
 	}
 
Index: lib/ssl_openssl.c
===================================================================
--- lib/ssl_openssl.c	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ lib/ssl_openssl.c	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -56,4 +56,10 @@
 static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
 
+
+void ssl_init( void );
+{
+	initialized = TRUE;
+	SSLeay_add_ssl_algorithms();
+}
 
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
@@ -115,6 +121,5 @@
 	if( !initialized )
 	{
-		initialized = TRUE;
-		SSLeay_add_ssl_algorithms();
+		ssl_init();
 	}
 	
Index: otr.c
===================================================================
--- otr.c	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ otr.c	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -112,5 +112,5 @@
 
 /* main function for the forked keygen slave */
-void keygen_child_main(OtrlUserState us, int infd, int outfd);
+void keygen_child_main(const char *nick, int infd, int outfd);
 
 /* mainloop handler for when a keygen finishes */
@@ -1527,5 +1527,5 @@
 			/* child process */
 			signal(SIGTERM, exit);
-			keygen_child_main(irc->otr->us, to[0], from[1]);
+			keygen_child_main(irc->nick, to[0], from[1]);
 			exit(0);
 		}
@@ -1548,6 +1548,6 @@
 			kg=&((*kg)->next);
 		*kg = g_new0(kg_t, 1);
-		(*kg)->accountname = handle;
-		(*kg)->protocol = protocol;
+		(*kg)->accountname = g_strdup(handle);
+		(*kg)->protocol = g_strdup(protocol);
 	} else {
 		/* send our job over and remember it */
@@ -1555,15 +1555,22 @@
 		fprintf(irc->otr->to, "%s\n%s\n", handle, protocol);
 		fflush(irc->otr->to);
-		irc->otr->sent_accountname = handle;
-		irc->otr->sent_protocol = protocol;
-	}
-}
-
-void keygen_child_main(OtrlUserState us, int infd, int outfd)
-{
+		irc->otr->sent_accountname = g_strdup(handle);
+		irc->otr->sent_protocol = g_strdup(protocol);
+	}
+}
+
+void keygen_child_main(const char *nick, int infd, int outfd)
+{
+	OtrlUserState us;
+	char *kf;
 	FILE *input, *output;
 	char filename[128], accountname[512], protocol[512];
 	gcry_error_t e;
 	int tempfd;
+	
+	us = otrl_userstate_create();
+	kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, nick);
+	otrl_privkey_read(us, kf);
+	g_free(kf);
 	
 	input = fdopen(infd, "r");
@@ -1620,4 +1627,6 @@
 	
 	/* forget this job */
+	g_free(irc->otr->sent_accountname);
+	g_free(irc->otr->sent_protocol);
 	irc->otr->sent_accountname = NULL;
 	irc->otr->sent_protocol = NULL;
Index: otr.h
===================================================================
--- otr.h	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ otr.h	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -51,6 +51,6 @@
 /* representing a keygen job */
 typedef struct kg {
-	const char *accountname;
-	const char *protocol;
+	char *accountname;
+	char *protocol;
 	
 	struct kg *next;
@@ -65,6 +65,6 @@
 	
 	/* active keygen job (NULL if none) */
-	const char *sent_accountname;
-	const char *sent_protocol;
+	char *sent_accountname;
+	char *sent_protocol;
 	
 	/* keygen jobs waiting to be sent to slave */
Index: unix.c
===================================================================
--- unix.c	(revision fd9fa52e0014459079444bd7bfff7a40eef4e27a)
+++ unix.c	(revision ba5add72f824504a21eb780cae638c3ea2166ba0)
@@ -31,4 +31,5 @@
 #include "help.h"
 #include "ipc.h"
+#include "lib/ssl_client.h"
 #include <signal.h>
 #include <unistd.h>
@@ -55,4 +56,10 @@
 	b_main_init();
 	nogaim_init();
+	/* Ugly Note: libotr and gnutls both use libgcrypt. libgcrypt
+	   has a process-global config state whose initialization happpens
+	   twice if libotr and gnutls are used together. libotr installs custom
+	   memory management functions for libgcrypt while our gnutls module
+	   uses the defaults. Therefore we initialize OTR after SSL. *sigh* */
+	ssl_init();
 	otr_init();
 	
