54d7df0c7b00f3e8f10684e6ead84cb2c0b72b3a
diff --git a/libsoup/soup-cookie-jar.c b/libsoup/soup-cookie-jar.c
index 3b39f6c..3dea402 100644
--- a/libsoup/soup-cookie-jar.c
+++ b/libsoup/soup-cookie-jar.c
@@ -208,23 +208,8 @@ static GSList *
 get_cookies_for_domain (SoupCookieJar *jar, const char *domain)
 {
 	SoupCookieJarPrivate *priv = SOUP_COOKIE_JAR_GET_PRIVATE (jar);
-	GSList *cookies, *orig_cookies, *c;
-	SoupCookie *cookie;
 
-	cookies = g_hash_table_lookup (priv->domains, domain);
-	c = orig_cookies = cookies;
-	while (c) {
-		cookie = c->data;
-		c = c->next;
-		if (cookie->expires && soup_date_is_past (cookie->expires)) {
-			cookies = g_slist_remove (cookies, cookie);
-			soup_cookie_free (cookie);
-		}
-	}
-
-	if (cookies != orig_cookies)
-		g_hash_table_insert (priv->domains, g_strdup (domain), cookies);
-	return cookies;
+	return g_hash_table_lookup (priv->domains, domain);
 }
 
 /**
@@ -251,16 +236,27 @@ soup_cookie_jar_add_cookie (SoupCookieJar *jar, SoupCookie *cookie)
 	for (oc = old_cookies; oc; oc = oc->next) {
 		old_cookie = oc->data;
 		if (!strcmp (cookie->name, old_cookie->name)) {
-			/* The new cookie is a replacement for an old
-			 * cookie. It might be pre-expired, but we
-			 * don't worry about that here;
-			 * get_cookies_for_domain() will delete it
-			 * later.
-			 */
-			oc->data = cookie;
-			g_signal_emit (jar, signals[CHANGED], 0,
-				       old_cookie, cookie);
-			soup_cookie_free (old_cookie);
+			if (cookie->expires && soup_date_is_past (cookie->expires)) {
+				/* The new cookie has an expired date,
+				 * this is the way the the server has
+				 * of telling us that we have to
+				 * remove the cookie.
+				 */
+				old_cookies = g_slist_delete_link (old_cookies, oc);
+				g_hash_table_insert (priv->domains,
+						     g_strdup (cookie->domain),
+						     old_cookies);
+				g_signal_emit (jar, signals[CHANGED], 0,
+					       old_cookie, NULL);
+				soup_cookie_free (old_cookie);
+				soup_cookie_free (cookie);
+			} else {
+				oc->data = cookie;
+				g_signal_emit (jar, signals[CHANGED], 0,
+					       old_cookie, cookie);
+				soup_cookie_free (old_cookie);
+			}
+
 			return;
 		}
 		prev = oc;
