Index: protocols/twitter/twitter.c
===================================================================
--- protocols/twitter/twitter.c	(revision 62d2cfb0b7b5e7f3eda9ca13b1877d3ad74fcd5e)
+++ protocols/twitter/twitter.c	(revision 2abceca711403e8e3308213954b4477ceecd4282)
@@ -35,5 +35,7 @@
 	struct im_connection *ic = data;
 	// Check if we are still logged in...
-	if ((ic->flags & OPT_LOGGED_IN) != OPT_LOGGED_IN)
+	// We are logged in if the flag says so and the connection is still in the connections list.
+	if ((ic->flags & OPT_LOGGED_IN) != OPT_LOGGED_IN 
+			&& !g_slist_find( twitter_connections, ic ))
 		return 0;
 
@@ -55,4 +57,5 @@
 	set_t *s;
 	s = set_add( &acc->set, "use_groupchat", "false", set_eval_bool, acc );
+	s->flags |= ACC_SET_OFFLINE_ONLY;
 }
 
@@ -72,6 +75,6 @@
 	ic->proto_data = td;
 
-	// Set the status to logged in.
-	ic->flags = OPT_LOGGED_IN;
+	imcb_log( ic, "Connecting to twitter" );
+	imcb_connected(ic);
 
 	// Run this once. After this queue the main loop function.
@@ -79,8 +82,6 @@
 
 	// Queue the main_loop
-	b_timeout_add(60000, twitter_main_loop, ic);
-
-	imcb_log( ic, "Connecting to twitter" );
-	imcb_connected(ic);
+	// Save the return value, so we can remove the timeout on logout.
+	td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
 
 	twitter_connections = g_slist_append( twitter_connections, ic );
@@ -96,4 +97,7 @@
 	// Set the status to logged out.
 	ic->flags = 0;
+
+	// Remove the main_loop function from the function queue.
+	b_event_remove(td->main_loop_id);
 
 	if( td )
@@ -149,4 +153,6 @@
 static void twitter_chat_msg( struct groupchat *c, char *message, int flags )
 {
+	if( c && message )
+		twitter_post_status(c->ic, message);
 }
 
Index: protocols/twitter/twitter.h
===================================================================
--- protocols/twitter/twitter.h	(revision 62d2cfb0b7b5e7f3eda9ca13b1877d3ad74fcd5e)
+++ protocols/twitter/twitter.h	(revision 2abceca711403e8e3308213954b4477ceecd4282)
@@ -38,4 +38,5 @@
 	char* pass;
 	guint64 home_timeline_id;
+	gint main_loop_id;
 	struct groupchat *home_timeline_gc;
 };
Index: protocols/twitter/twitter_http.c
===================================================================
--- protocols/twitter/twitter_http.c	(revision 62d2cfb0b7b5e7f3eda9ca13b1877d3ad74fcd5e)
+++ protocols/twitter/twitter_http.c	(revision 2abceca711403e8e3308213954b4477ceecd4282)
@@ -39,7 +39,5 @@
 
 
-char *twitter_urlencode(const char *instr);
 char *twitter_url_append(char *url, char *key, char* value);
-static int isurlchar(unsigned char c);
 
 /**
@@ -107,7 +105,7 @@
 	// Make the request.
 	request = g_strdup_printf(  "%s %s HTTP/1.0\r\n"
-								"Host: %s\r\n"
-								"User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
-								is_post ? "POST" : "GET", url->file, url->host );
+	                            "Host: %s\r\n"
+	                            "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
+	                            is_post ? "POST" : "GET", url->file, url->host );
 
 	// If a pass and user are given we append them to the request.
@@ -146,6 +144,9 @@
 char *twitter_url_append(char *url, char *key, char* value)
 {
-	char *key_encoded = twitter_urlencode(key);
-	char *value_encoded = twitter_urlencode(value);
+	char *key_encoded = g_strndup(key, 3 * strlen(key));
+	http_encode(key_encoded);
+	char *value_encoded = g_strndup(value, 3 * strlen(value));
+	http_encode(value_encoded);
+
 	char *retval;
 	if (strlen(url) != 0)
@@ -159,33 +160,4 @@
 	return retval;
 }
-
-char *twitter_urlencode(const char *instr)
-{
-	int ipos=0, bpos=0;
-	char *str = NULL;
-	int len = strlen(instr);
-
-	if(!(str = g_new(char, 3*len + 1) ))
-		return "";
-
-	while(instr[ipos]) {
-		while(isurlchar(instr[ipos]))
-			str[bpos++] = instr[ipos++];
-		if(!instr[ipos])
-			break;
-
-		g_snprintf(&str[bpos], 4, "%%%.2x", instr[ipos]);
-		bpos+=3;
-		ipos++;
-	}
-	str[bpos]='\0';
-
-	/* free extra alloc'ed mem. */
-	len = strlen(str);
-	str = g_renew(char, str, len+1);
-
-	return (str);
-}
-
 
 char *twitter_urldecode(const char *instr)
@@ -229,7 +201,2 @@
 }
 
-static int isurlchar(unsigned char c)
-{
-	return (isalnum(c) || '-' == c || '_' == c);
-}
-
Index: protocols/twitter/twitter_lib.c
===================================================================
--- protocols/twitter/twitter_lib.c	(revision 62d2cfb0b7b5e7f3eda9ca13b1877d3ad74fcd5e)
+++ protocols/twitter/twitter_lib.c	(revision 2abceca711403e8e3308213954b4477ceecd4282)
@@ -65,4 +65,5 @@
 	g_free(txu->name);
 	g_free(txu->screen_name);
+	g_free(txu);
 }
 
@@ -76,4 +77,5 @@
 	g_free(txs->text);
 	txu_free(txs->user);
+	g_free(txs);
 }
 
@@ -131,5 +133,5 @@
 {
 	// Do something with the cursor.
-	txl->next_cursor = atoi(node->text);
+	txl->next_cursor = node->text != NULL ? atoi(node->text) : -1;
 
 	return XT_HANDLED;
@@ -153,5 +155,5 @@
 		{
 			// Add the item to the list.
-			txl->list = g_slist_append (txl->list, g_memdup( node->text, node->text_len + 1 ));
+			txl->list = g_slist_append (txl->list, g_memdup( child->text, child->text_len + 1 ));
 		}
 		else if ( g_strcasecmp( "next_cursor", child->name ) == 0)
@@ -187,5 +189,4 @@
 
 	txl = g_new0(struct twitter_xml_list, 1);
-	txl->list = NULL;
 
 	// Parse the data.
@@ -451,5 +452,5 @@
 	if (req->status_code != 200) {
 		// It didn't go well, output the error and return.
-		imcb_error(ic, "Could not retrieve home/timeline. HTTP STATUS: %d", req->status_code);
+		imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ". HTTP STATUS: %d", req->status_code);
 		return;
 	}
@@ -488,4 +489,6 @@
 	struct xt_parser *parser;
 	struct twitter_xml_list *txl;
+	GSList *l = NULL;
+	struct twitter_xml_user *user;
 
 	// Check if the connection is still active.
@@ -496,5 +499,5 @@
 	if (req->status_code != 200) {
 		// It didn't go well, output the error and return.
-		imcb_error(ic, "Could not retrieve home/timeline. HTTP STATUS: %d", req->status_code);
+		imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code);
 		return;
 	}
@@ -511,6 +514,4 @@
 	xt_free( parser );
 
-	GSList *l = NULL;
-	struct twitter_xml_user *user;
 	// Add the users as buddies.
 	for ( l = txl->list; l ; l = g_slist_next(l) )
@@ -559,6 +560,5 @@
 	if (req->status_code != 200) {
 		// It didn't go well, output the error and return.
-		imcb_error(ic, "Could not post tweed... HTTP STATUS: %d", req->status_code);
-		imcb_error(ic, req->reply_body);
+		imcb_error(ic, "Could not post tweet... HTTP STATUS: %d", req->status_code);
 		return;
 	}
