diff --git a/configure.ac b/configure.ac
index 19e1b0a..84180d1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -301,7 +301,7 @@ AC_CHECK_TYPE(socklen_t,,
 #endif
 )
 AC_CHECK_HEADERS(sys/sockio.h)
-AC_CHECK_FUNCS([setresuid setenv unsetenv clearenv strrep])
+AC_CHECK_FUNCS([setresuid setenv unsetenv clearenv])
 
 dnl checks needed for Darwin compatibility to linux **environ.
 AC_CHECK_HEADERS(crt_externs.h)
diff --git a/daemon/gdm-local-display-factory.c b/daemon/gdm-local-display-factory.c
index 5e59565..c1d48ff 100644
--- a/daemon/gdm-local-display-factory.c
+++ b/daemon/gdm-local-display-factory.c
@@ -542,40 +542,6 @@ on_display_status_changed (GdmDisplay             *display,
         }
 }
 
-#ifndef HAVE_STRREP
-static void
-strrep (char* in, char** out, char* old, char* new)
-{
-        char* temp;
-        char* found = strstr(in, old);
-        if(!found) {
-                *out = malloc(strlen(in) + 1);
-                strcpy(*out, in);
-                return;
-        }
-
-        int idx = found - in;
-
-        *out = realloc(*out, strlen(in) - strlen(old) + strlen(new) + 1);
-        strncpy(*out, in, idx);
-        strcpy(*out + idx, new);
-        strcpy(*out + idx + strlen(new), in + idx + strlen(old));
-
-        temp = malloc(idx+strlen(new)+1);
-        strncpy(temp,*out,idx+strlen(new));
-        temp[idx + strlen(new)] = '\0';
-
-        free (*out);
-        *out = NULL;
-
-        strrep(found + strlen(old), out, old, new);
-        temp = realloc(temp, strlen(temp) + strlen(*out) + 1);
-        strcat(temp,*out);
-        free(*out);
-        *out = temp; 
-}
-#endif
-
 static void
 seat_open_session_request (DBusGProxy             *seat_proxy,
                            const char             *ssid,
@@ -591,11 +557,10 @@ seat_open_session_request (DBusGProxy             *seat_proxy,
         gchar     **argv;
         GError     *error;
         char       *comm = NULL;
-        char       *comm_tmp = NULL;
         const char *sid = NULL;
         gint32      display_number;
-        gboolean    is_chooser;
-        gboolean    use_auth;
+        gboolean    is_chooser = FALSE;
+        gboolean    use_auth = FALSE;
         int         i;
         char       *xserver_command;
         gboolean    display_is_blocked;
@@ -626,50 +591,49 @@ seat_open_session_request (DBusGProxy             *seat_proxy,
                 return;
         }
 
-        comm = g_strdup (xserver_command);
+        g_debug("GdmLocalDisplayFactory: X11 server cmd pre-mangle: %s",
+                xserver_command);
         for (i = 0; i < argc; i++) {
                 /* replase $display in case of not specified */
                 if (g_str_equal (argv[i], "$display")) {
                         display_number = take_next_display_number (factory);
-                        comm_tmp = NULL;
-                        strrep (comm, &comm_tmp, "$display", "");
-                        g_free (comm);
-                        comm = comm_tmp;
-                        break;
+                        argv[i][0] = '\0';
+                        continue;
                 }
 
                 /* get display_number in case of specified */
                 if (g_str_has_prefix (argv[i], ":")) {
                         display_number = atoi (argv[i]+1);
-                        comm_tmp = NULL;
-                        strrep (comm, &comm_tmp, argv[i], "");
-                        g_free (comm);
-                        comm = comm_tmp;
-                        break;
+                        argv[i][0] = '\0';
+                        continue;
                 }
-        }
-        g_strfreev (argv);
 
-        is_chooser = FALSE;
-        if (strstr (comm, "-indirect")) {
-                is_chooser = TRUE;
-        }
+                if (! g_strcmp0 (argv[i], "-indirect")) {
+                        is_chooser = TRUE;
+                        continue;
+                }
 
-        use_auth = FALSE;
-        if (strstr (comm, "-auth $auth")) {
-                use_auth = TRUE;
-                comm_tmp = NULL;
-                strrep (comm, &comm_tmp, "-auth $auth", "");
-                g_free (comm);
-                comm = comm_tmp;
-        }
+                /*
+                 * -auth is added by gdm_server_resolve_command_line()
+                 * in gdm-server.c
+                 */
+                if (! g_strcmp0 (argv[i], "-auth") &&
+                    ! g_strcmp0 (argv[i+1], "$auth")) {
+                        use_auth = TRUE;
+                        argv[i][0] = '\0';
+                        argv[++i][0] = '\0';
+                        continue;
+                }
 
-        if (strstr (comm, "$vt")) {
-                comm_tmp = NULL;
-                strrep (comm, &comm_tmp, "$vt", "");
-                g_free (comm);
-                comm = comm_tmp;
+                if (!g_strcmp0 (argv[i], "$vt")) {
+                        argv[i][0] = '\0';
+                        continue;
+                }
         }
+        comm = g_strjoinv (" ", argv);
+        g_debug ("GdmLocalDisplayFactory: X11 server cmd post-mangle: %s",
+                 comm);
+        g_strfreev (argv);
 
         if (display == NULL) {
                 if (is_chooser) {
