diff --git a/libtcc.c b/libtcc.c
index 3494ee20..c25fb2c1 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -509,32 +509,6 @@ char *tcc_fileextension (const char *name)
     return e ? e : strchr(b, 0);
 }
 
-#ifdef _WIN32
-char *normalize_slashes(char *path)
-{
-    char *p;
-    for (p = path; *p; ++p)
-        if (*p == '\\')
-            *p = '/';
-    return path;
-}
-
-void tcc_set_lib_path_w32(TCCState *s)
-{
-    /* on win32, we suppose the lib and includes are at the location
-       of 'tcc.exe' */
-    char path[1024], *p;
-    GetModuleFileNameA(NULL, path, sizeof path);
-    p = tcc_basename(normalize_slashes(strlwr(path)));
-    if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
-        p -= 5;
-    else if (p > path)
-        p--;
-    *p = 0;
-    tcc_set_lib_path(s, path);
-}
-#endif
-
 void set_pages_executable(void *ptr, unsigned long length)
 {
 #ifdef _WIN32
@@ -1867,9 +1841,12 @@ TCCState *tcc_new(void)
     if (!s)
         return NULL;
     tcc_state = s;
+#ifdef _WIN32
+    tcc_set_lib_path_w32(s);
+#else
+    tcc_set_lib_path(s, CONFIG_TCCDIR);
+#endif
     s->output_type = TCC_OUTPUT_MEMORY;
-    s->tcc_lib_path = CONFIG_TCCDIR;
-
     preprocess_new();
 
     /* we add dummy defines for some special macros to speed up tests
@@ -1993,6 +1970,7 @@ void tcc_delete(TCCState *s1)
     dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
     dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
 
+    tcc_free(s1->tcc_lib_path);
     tcc_free(s1);
 }
 
@@ -2348,6 +2326,7 @@ int tcc_set_flag(TCCState *s, const char *flag_name, int value)
 /* set CONFIG_TCCDIR at runtime */
 void tcc_set_lib_path(TCCState *s, const char *path)
 {
+    tcc_free(s->tcc_lib_path);
     s->tcc_lib_path = tcc_strdup(path);
 }
 
diff --git a/tcc.c b/tcc.c
index daa10135..cadb5e91 100644
--- a/tcc.c
+++ b/tcc.c
@@ -427,9 +427,7 @@ int main(int argc, char **argv)
     int64_t start_time = 0;
 
     s = tcc_new();
-#ifdef _WIN32
-    tcc_set_lib_path_w32(s);
-#endif
+
     output_type = TCC_OUTPUT_EXE;
     outfile = NULL;
     multiple_files = 1;
diff --git a/tcc.h b/tcc.h
index 50bbd56a..bf242e5f 100644
--- a/tcc.h
+++ b/tcc.h
@@ -473,7 +473,7 @@ struct TCCState {
     /* compile with built-in memory and bounds checker */
     int do_bounds_check;
     /* give the path of the tcc libraries */
-    const char *tcc_lib_path;
+    char *tcc_lib_path;
 
     /* error handling */
     void *error_opaque;
diff --git a/tccpe.c b/tccpe.c
index fe87919f..652ab069 100644
--- a/tccpe.c
+++ b/tccpe.c
@@ -1511,7 +1511,7 @@ PUB_FN int pe_load_file(struct TCCState *s1, const char *filename, int fd)
         else if (pe_load_res(s1, fp) == 0)
             ret = 0;
         else if (read_mem(fp, 0, buf, sizeof buf) && 0 == strncmp(buf, "MZ", 2))
-            ret = pe_load_dll(s1, filename, fp);
+            ret = pe_load_dll(s1, tcc_basename(filename), fp);
         fclose(fp);
     }
     return ret;