mkpath will fail if directory already exists -> fixed by adding -d check