diff --git a/core/stream_interface.c b/core/stream_interface.c
index da0c733..89525a5 100644
--- a/core/stream_interface.c
+++ b/core/stream_interface.c
@@ -525,6 +525,10 @@ void *network_initializer(void *data)
 		notify(START, RECOVERY_NO_ERROR, INFOLEVEL, "Software Update started !");
 		TRACE("Software update started");
 
+		/* Create directories for scripts/datadst */
+		swupdate_create_directory(SCRIPTS_DIR_SUFFIX);
+		swupdate_create_directory(DATADST_DIR_SUFFIX);
+
 		req = &inst.req;
 
 		/*
@@ -657,6 +661,11 @@ void *network_initializer(void *data)
 
 		/* release temp files we may have created */
 		cleanup_files(software);
+
+#ifndef CONFIG_NOCLEANUP
+		swupdate_remove_directory(SCRIPTS_DIR_SUFFIX);
+		swupdate_remove_directory(DATADST_DIR_SUFFIX);
+#endif
 	}
 
 	pthread_exit((void *)0);
diff --git a/core/swupdate.c b/core/swupdate.c
index 949a647..a31aeb1 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -23,7 +23,6 @@
 #include <pthread.h>
 #include <signal.h>
 #include <sys/wait.h>
-#include <ftw.h>
 
 #include "bsdqueue.h"
 #include "cpiohdr.h"
@@ -244,53 +243,6 @@ static int parse_image_selector(const char *selector, struct swupdate_cfg *sw)
 	return 0;
 }
 
-static void create_directory(const char* path) {
-	char* dpath;
-	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
-		ENOMEM_ASPRINTF) {
-		ERROR("OOM: Directory %s not created", path);
-		return;
-	}
-	if (mkdir(dpath, 0777)) {
-		WARN("Directory %s cannot be created due to : %s",
-		     path, strerror(errno));
-	}
-	free(dpath);
-}
-
-#ifndef CONFIG_NOCLEANUP
-static int _remove_directory_cb(const char *fpath, const struct stat *sb,
-                                int typeflag, struct FTW *ftwbuf)
-{
-	(void)sb;
-	(void)typeflag;
-	(void)ftwbuf;
-	return remove(fpath);
-}
-
-static int remove_directory(const char* path)
-{
-	char* dpath;
-	int ret;
-	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
-		ENOMEM_ASPRINTF) {
-		ERROR("OOM: Directory %s not removed", path);
-		return -ENOMEM;
-	}
-	ret = nftw(dpath, _remove_directory_cb, 64, FTW_DEPTH | FTW_PHYS);
-	free(dpath);
-	return ret;
-}
-#endif
-
-static void swupdate_cleanup(void)
-{
-#ifndef CONFIG_NOCLEANUP
-	remove_directory(SCRIPTS_DIR_SUFFIX);
-	remove_directory(DATADST_DIR_SUFFIX);
-#endif
-}
-
 static void swupdate_init(struct swupdate_cfg *sw)
 {
 	/* Initialize internal tree to store configuration */
@@ -303,15 +255,6 @@ static void swupdate_init(struct swupdate_cfg *sw)
 	LIST_INIT(&sw->extprocs);
 	sw->cert_purpose = SSL_PURPOSE_DEFAULT;
 
-
-	/* Create directories for scripts */
-	create_directory(SCRIPTS_DIR_SUFFIX);
-	create_directory(DATADST_DIR_SUFFIX);
-
-	if (atexit(swupdate_cleanup) != 0) {
-		TRACE("Cannot setup SWUpdate cleanup on exit");
-	}
-
 #ifdef CONFIG_MTD
 	mtd_init();
 	ubi_init();
diff --git a/core/util.c b/core/util.c
index 6188650..7e96652 100644
--- a/core/util.c
+++ b/core/util.c
@@ -12,6 +12,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <ftw.h>
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/param.h>
@@ -146,6 +147,45 @@ const char* get_tmpdirscripts(void)
 	return TMPDIRSCRIPT;
 }
 
+void swupdate_create_directory(const char* path) {
+	char* dpath;
+	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
+		ENOMEM_ASPRINTF) {
+		ERROR("OOM: Directory %s not created", path);
+		return;
+	}
+	if (mkdir(dpath, 0777)) {
+		WARN("Directory %s cannot be created due to : %s",
+			 path, strerror(errno));
+	}
+	free(dpath);
+}
+
+#ifndef CONFIG_NOCLEANUP
+static int _remove_directory_cb(const char *fpath, const struct stat *sb,
+								int typeflag, struct FTW *ftwbuf)
+{
+	(void)sb;
+	(void)typeflag;
+	(void)ftwbuf;
+	return remove(fpath);
+}
+
+int swupdate_remove_directory(const char* path)
+{
+	char* dpath;
+	int ret;
+	if (asprintf(&dpath, "%s%s", get_tmpdir(), path) ==
+		ENOMEM_ASPRINTF) {
+		ERROR("OOM: Directory %s not removed", path);
+		return -ENOMEM;
+	}
+	ret = nftw(dpath, _remove_directory_cb, 64, FTW_DEPTH | FTW_PHYS);
+	free(dpath);
+	return ret;
+}
+#endif
+
 char **splitargs(char *args, int *argc)
 {
 	char **argv = NULL;
diff --git a/include/util.h b/include/util.h
index 9f29f5f..a817f1c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -245,6 +245,11 @@ unsigned long long ustrtoull(const char *cp, unsigned int base);
 const char* get_tmpdir(void);
 const char* get_tmpdirscripts(void);
 
+void swupdate_create_directory(const char* path);
+#ifndef CONFIG_NOCLEANUP
+int swupdate_remove_directory(const char* path);
+#endif
+
 int swupdate_mount(const char *device, const char *dir, const char *fstype);
 int swupdate_umount(const char *dir);
 
