diff --git a/matlab/CheckPath.m b/matlab/CheckPath.m
index fdaa2dd3ae2c6e5a4b0be31182d3ebedb792bb94..4513c3b8a2d5be04ca30bdcb818ca5edb55b4993 100644
--- a/matlab/CheckPath.m
+++ b/matlab/CheckPath.m
@@ -1,8 +1,9 @@
-function DirectoryName = CheckPath(type)
+function DirectoryName = CheckPath(type,dname)
 % Creates the subfolder "./M_.dname/type" if it does not exist yet.
 %
 % INPUTS
-%    type  [string]    Name of the subfolder. 
+%    type   [string]    Name of the subfolder. 
+%    dname  [string]    Name of the directory
 %
 % OUTPUTS
 %    none.
@@ -27,14 +28,12 @@ function DirectoryName = CheckPath(type)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-global M_
+DirectoryName = [ dname '/' type ];
 
-DirectoryName = [ M_.dname '/' type ];
-
-if ~isdir(M_.dname)
+if ~isdir(dname)
     % Make sure there isn't a file with the same name, see trac ticket #47
-    delete(M_.dname)
-    mkdir('.', M_.dname);
+    delete(dname)
+    mkdir('.', dname);
 end
 
 if ~isdir(DirectoryName)
diff --git a/matlab/CutSample.m b/matlab/CutSample.m
index 18595631c9a37ef1ca0d5701f5837b5f8e3c7a0a..7beb9993d6856ba9b5d5b03d3ff787e63e4617cf 100644
--- a/matlab/CutSample.m
+++ b/matlab/CutSample.m
@@ -33,7 +33,7 @@ function CutSample(M_, options_, estim_params_)
 
 npar = estim_params_.np+estim_params_.nvn+estim_params_.ncx+estim_params_.ncn+estim_params_.nvx;
 
-DirectoryName = CheckPath('metropolis');
+DirectoryName = CheckPath('metropolis',M_.dname);
 file = dir([ DirectoryName ,filesep,  M_.fname '_mh_history.mat']);
 files = dir([ DirectoryName ,filesep, M_.fname '_mh*.mat' ]);
 if ~length(files)
diff --git a/matlab/GetAllPosteriorDraws.m b/matlab/GetAllPosteriorDraws.m
index 49bb8a99445e548e62fc41493d1edc6c1dea736e..3a3a489ecbe6d873ff8c8027f5185353df3bf3a0 100644
--- a/matlab/GetAllPosteriorDraws.m
+++ b/matlab/GetAllPosteriorDraws.m
@@ -39,7 +39,7 @@ nblck = options_.mh_nblck;
 
 iline = FirstLine;
 linee = 1;
-DirectoryName = CheckPath('metropolis');
+DirectoryName = CheckPath('metropolis',M_.dname);
 
 if nblck>1 && nargin<6
     Draws = zeros(NumberOfDraws*nblck,1);
diff --git a/matlab/GetPosteriorParametersStatistics.m b/matlab/GetPosteriorParametersStatistics.m
index 3d69fd49fba05747c44637e46a6629587b98d60f..3df62968b99a96d4178a6e66abefe9199ee3e82e 100644
--- a/matlab/GetPosteriorParametersStatistics.m
+++ b/matlab/GetPosteriorParametersStatistics.m
@@ -45,8 +45,8 @@ ncn     = estim_params_.ncn;
 np      = estim_params_.np ;
 nx      = nvx+nvn+ncx+ncn+np;
 
-DirectoryName = CheckPath('metropolis');
-OutputDirectoryName = CheckPath('Output');
+DirectoryName = CheckPath('metropolis',M_.dname);
+OutputDirectoryName = CheckPath('Output',M_.dname);
 
 load([ DirectoryName '/'  M_.fname '_mh_history'])
 FirstMhFile = record.KeepedDraws.FirstMhFile;
diff --git a/matlab/McMCDiagnostics.m b/matlab/McMCDiagnostics.m
index 29ffc96184936415d3ad64b0993cfd85e8109856..de8974f0483d0863adf1abf936d4247a42f507bb 100644
--- a/matlab/McMCDiagnostics.m
+++ b/matlab/McMCDiagnostics.m
@@ -33,8 +33,8 @@ function McMCDiagnostics(options_, estim_params_, M_)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-DirectoryName = CheckPath('Output');
-MhDirectoryName = CheckPath('metropolis');
+DirectoryName = CheckPath('Output',M_.dname);
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 
 TeX = options_.TeX;
 nblck = options_.mh_nblck;
diff --git a/matlab/McMCDiagnostics_core.m b/matlab/McMCDiagnostics_core.m
index 0c398703dcc322deedb3fb1384603635abfae0dc..540c322e6f0cf7ac8673dd519b4261542428daef 100644
--- a/matlab/McMCDiagnostics_core.m
+++ b/matlab/McMCDiagnostics_core.m
@@ -56,7 +56,7 @@ if whoiam
     Parallel=myinputs.Parallel;
 end
 if ~exist('MhDirectoryName'),
-    MhDirectoryName = CheckPath('metropolis');
+    MhDirectoryName = CheckPath('metropolis',M_.dname);
 end
 
 ALPHA = 0.2;                                % increase too much with the number of simulations.
diff --git a/matlab/PlotPosteriorDistributions.m b/matlab/PlotPosteriorDistributions.m
index 53de1924a3502034a451bb81a2a5f097243cd22f..b0daaf69b2e3a99e84b65ee75f0f4802070b172e 100644
--- a/matlab/PlotPosteriorDistributions.m
+++ b/matlab/PlotPosteriorDistributions.m
@@ -33,7 +33,7 @@ function oo_ = PlotPosteriorDistributions(estim_params_, M_, options_, bayestopt
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-OutputDirectoryName = CheckPath('Output');
+OutputDirectoryName = CheckPath('Output',M_.dname);
 
 TeX     = options_.TeX;
 nblck   = options_.mh_nblck;
diff --git a/matlab/PosteriorFilterSmootherAndForecast.m b/matlab/PosteriorFilterSmootherAndForecast.m
index 7836974d8915b95af9b0cc3c64d98166367bc98a..4cce1159e5a9c8068ec79b80c5e5b7df05dec99e 100644
--- a/matlab/PosteriorFilterSmootherAndForecast.m
+++ b/matlab/PosteriorFilterSmootherAndForecast.m
@@ -55,8 +55,8 @@ i_last_obs = gend+(1-M_.maximum_endo_lag:0);
 horizon = options_.forecast;
 maxlag = M_.maximum_endo_lag;
 %%
-CheckPath('Plots/');
-DirectoryName = CheckPath('metropolis');
+CheckPath('Plots/',M_.dname);
+DirectoryName = CheckPath('metropolis',M_.dname);
 load([ DirectoryName '/'  M_.fname '_mh_history.mat'])
 FirstMhFile = record.KeepedDraws.FirstMhFile;
 FirstLine = record.KeepedDraws.FirstLine;
diff --git a/matlab/PosteriorIRF.m b/matlab/PosteriorIRF.m
index fdbf10141720fc32109118b1f4b54f3d45e6e010..c70b05f82a23a302727f89da9534186eebd5c34f 100644
--- a/matlab/PosteriorIRF.m
+++ b/matlab/PosteriorIRF.m
@@ -76,17 +76,17 @@ else
     MAX_nirfs_dsgevar = 0;
 end
 
-DirectoryName = CheckPath('Output');
+DirectoryName = CheckPath('Output',M_.dname);
 if strcmpi(type,'posterior')
-    MhDirectoryName = CheckPath('metropolis');
+    MhDirectoryName = CheckPath('metropolis',M_.dname);
 elseif strcmpi(type,'gsa')
     if options_.opt_gsa.pprior
-        MhDirectoryName = CheckPath(['GSA' filesep 'prior']);
+        MhDirectoryName = CheckPath(['GSA' filesep 'prior'],M_.dname);
     else
-        MhDirectoryName = CheckPath(['GSA' filesep 'mc']);
+        MhDirectoryName = CheckPath(['GSA' filesep 'mc'],M_.dname);
     end
 else
-    MhDirectoryName = CheckPath('prior');
+    MhDirectoryName = CheckPath('prior',M_.dname);
 end
 delete([MhDirectoryName filesep M_.fname '_IRF_DSGEs*.mat']);
 delete([MhDirectoryName filesep M_.fname '_IRF_BVARDSGEs*.mat']);
@@ -95,7 +95,7 @@ if strcmpi(type,'posterior')
     TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
     NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
 elseif strcmpi(type,'gsa')
-    RootDirectoryName = CheckPath('gsa');
+    RootDirectoryName = CheckPath('gsa',M_.dname);
     if options_.opt_gsa.pprior
         load([ RootDirectoryName filesep  M_.fname '_prior.mat'],'lpmat0','lpmat','istable')
     else
diff --git a/matlab/PosteriorIRF_core1.m b/matlab/PosteriorIRF_core1.m
index 068ecb2a0a0f90ec4d6a63d787c6cb040f52d9db..861772d7527a1fe09bbc5f475dfbfbae1f035f2e 100644
--- a/matlab/PosteriorIRF_core1.m
+++ b/matlab/PosteriorIRF_core1.m
@@ -84,15 +84,15 @@ end
 
 % MhDirectoryName = myinputs.MhDirectoryName;
 if strcmpi(type,'posterior')
-    MhDirectoryName = CheckPath('metropolis');
+    MhDirectoryName = CheckPath('metropolis',M_.dname);
 elseif strcmpi(type,'gsa')
     if options_.opt_gsa.pprior
-        MhDirectoryName = CheckPath(['gsa' filesep 'prior']);
+        MhDirectoryName = CheckPath(['gsa' filesep 'prior'],M_.dname);
     else
-        MhDirectoryName = CheckPath(['gsa' filesep 'mc']);
+        MhDirectoryName = CheckPath(['gsa' filesep 'mc'],M_.dname);
     end
 else
-    MhDirectoryName = CheckPath('prior');
+    MhDirectoryName = CheckPath('prior',M_.dname);
 end
 
 RemoteFlag = 0;
diff --git a/matlab/PosteriorIRF_core2.m b/matlab/PosteriorIRF_core2.m
index c1f1a81898284e36a70925ac0ef2dfa9d3593ce4..d2f27b691c8f7c40bf197bc026fd1ff5a29d33ce 100644
--- a/matlab/PosteriorIRF_core2.m
+++ b/matlab/PosteriorIRF_core2.m
@@ -69,7 +69,7 @@ end
 
 % To save the figures where the function is computed!
 
-DirectoryName = CheckPath('Output');
+DirectoryName = CheckPath('Output',M_.dname);
 
 RemoteFlag = 0;
 if whoiam,
diff --git a/matlab/ReshapeMatFiles.m b/matlab/ReshapeMatFiles.m
index 6ed8f1f2a62194ed59d02121282fea320b811fc7..fc99e4afbb36c23897d16c3fd191a104bd67491b 100644
--- a/matlab/ReshapeMatFiles.m
+++ b/matlab/ReshapeMatFiles.m
@@ -45,22 +45,22 @@ function ReshapeMatFiles(type, type2)
 global M_ options_
 
 if nargin==1, 
-    MhDirectoryName = [ CheckPath('metropolis') filesep ];
+    MhDirectoryName = [ CheckPath('metropolis',M_.dname) filesep ];
 else
     if strcmpi(type2,'posterior')
-        MhDirectoryName = [CheckPath('metropolis') filesep ];
+        MhDirectoryName = [CheckPath('metropolis',M_.dname) filesep ];
     elseif strcmpi(type2,'gsa')
         if options_.opt_gsa.morris==1,
-            MhDirectoryName = [CheckPath('gsa/screen') filesep ];
+            MhDirectoryName = [CheckPath('gsa/screen',M_.dname) filesep ];
         elseif options_.opt_gsa.morris==2,
-            MhDirectoryName = [CheckPath('gsa/identif') filesep ];
+            MhDirectoryName = [CheckPath('gsa/identif',M_.dname) filesep ];
         elseif options_.opt_gsa.pprior
-            MhDirectoryName = [CheckPath(['gsa' filesep 'prior']) filesep ];
+            MhDirectoryName = [CheckPath(['gsa' filesep 'prior'],M_.dname) filesep ];
         else
-            MhDirectoryName = [CheckPath(['gsa' filesep 'mc']) filesep ];
+            MhDirectoryName = [CheckPath(['gsa' filesep 'mc'],M_.dname) filesep ];
         end
     else
-        MhDirectoryName = [CheckPath('prior') filesep ];
+        MhDirectoryName = [CheckPath('prior',M_.dname) filesep ];
     end  
 end
 switch type
diff --git a/matlab/check_prior_analysis_data.m b/matlab/check_prior_analysis_data.m
index 2afff80984206ce5e22907129b4ed1f0b06ed939..e60d586d28d61aef193e013f6539a1401c99b995 100644
--- a/matlab/check_prior_analysis_data.m
+++ b/matlab/check_prior_analysis_data.m
@@ -68,7 +68,7 @@ switch type
   otherwise
     disp(['This feature is not yet implemented!'])
 end
-CheckPath('prior/moments');
+CheckPath('prior/moments',M_.dname);
 pdfinfo = dir([ M_.dname '/prior/' generic_prior_data_file_name '*']);
 if isempty(pdfinfo)
     info = 4;
diff --git a/matlab/compute_mh_covariance_matrix.m b/matlab/compute_mh_covariance_matrix.m
index 9c2949b728ac3ac1e1a3bc56793d31449c35252f..90c4a5e9d7fd54e13160463d9d22ce8d2bf5c0c8 100644
--- a/matlab/compute_mh_covariance_matrix.m
+++ b/matlab/compute_mh_covariance_matrix.m
@@ -42,7 +42,7 @@ n = estim_params_.np + ...
     estim_params_.nvx;
 nblck = options_.mh_nblck;
 
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 load([ MhDirectoryName '/'  M_.fname '_mh_history.mat'])
 
 FirstMhFile = record.KeepedDraws.FirstMhFile;
diff --git a/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m b/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m
index 7f53a8eb0689ab38e61b76a48e884700ec02bdb6..60c9aaf9f5db015f652d089e6196fee60ca45ab5 100644
--- a/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m
+++ b/matlab/dsge_simulated_theoretical_conditional_variance_decomposition.m
@@ -40,7 +40,7 @@ if strcmpi(type,'posterior')
     posterior = 1;
 elseif strcmpi(type,'prior')
     DrawsFiles = dir([M_.dname '/prior/draws/' type '_draws*' ]);
-    CheckPath('prior/moments');
+    CheckPath('prior/moments',M_.dname);
     posterior = 0;
 else
     disp('dsge_simulated_theoretical_conditional_variance_decomposition:: Unknown type!')
diff --git a/matlab/dsge_simulated_theoretical_correlation.m b/matlab/dsge_simulated_theoretical_correlation.m
index 2e4c751ff34a536e3150f32a1b537e51cc7783a7..d8a81a149c300da55ddfdbe04bba05219d2207a4 100644
--- a/matlab/dsge_simulated_theoretical_correlation.m
+++ b/matlab/dsge_simulated_theoretical_correlation.m
@@ -40,7 +40,7 @@ if strcmpi(type,'posterior')
     posterior = 1;
 elseif strcmpi(type,'prior')
     DrawsFiles = dir([M_.dname '/prior/draws/' type '_draws*' ]);
-    CheckPath('prior/moments');
+    CheckPath('prior/moments',M_.dname);
     posterior = 0;
 else
     disp('dsge_simulated_theoretical_correlation:: Unknown type!');
diff --git a/matlab/dsge_simulated_theoretical_covariance.m b/matlab/dsge_simulated_theoretical_covariance.m
index c4fe83bb6ce67688a5d4174c5617eb43bc1bd44d..1231b8b586adbbea8fd77067345dd9a6e8eaecdd 100644
--- a/matlab/dsge_simulated_theoretical_covariance.m
+++ b/matlab/dsge_simulated_theoretical_covariance.m
@@ -40,7 +40,7 @@ if strcmpi(type,'posterior')
     posterior = 1;
 elseif strcmpi(type,'prior')
     DrawsFiles = dir([M_.dname '/prior/draws/' type '_draws*' ]);
-    CheckPath('prior/moments');
+    CheckPath('prior/moments',M_.dname);
     posterior = 0;
 else
     disp('dsge_simulated_theoretical_covariance:: Unknown type!')
diff --git a/matlab/dsge_simulated_theoretical_variance_decomposition.m b/matlab/dsge_simulated_theoretical_variance_decomposition.m
index cf7d1297f6e446ee3088754083f8cb434983ad86..a4008b31abc7552958e486ea375fbe11a6280510 100644
--- a/matlab/dsge_simulated_theoretical_variance_decomposition.m
+++ b/matlab/dsge_simulated_theoretical_variance_decomposition.m
@@ -41,7 +41,7 @@ if strcmpi(type,'posterior')
     posterior = 1;
 elseif strcmpi(type,'prior')
     DrawsFiles = dir([M_.dname '/prior/draws/' type '_draws*' ]);
-    CheckPath('prior/moments');
+    CheckPath('prior/moments',M_.dname);
     posterior = 0;
 else
     disp('dsge_simulated_theoretical_variance_decomposition:: Unknown type!')
diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m
index 108b0dcdd6cbc683076cbb94f583e0434bab8076..0f8837171fc03b220f3ffe4db0a74f90b4c8ec7d 100644
--- a/matlab/dynare_estimation_1.m
+++ b/matlab/dynare_estimation_1.m
@@ -567,7 +567,7 @@ elseif ~any(bayestopt_.pshape > 0) && options_.mh_posterior_mode_estimation
 end
 
 
-OutputDirectoryName = CheckPath('Output');
+OutputDirectoryName = CheckPath('Output',M_.dname);
 
 if any(bayestopt_.pshape > 0) && options_.TeX %% Bayesian estimation (posterior mode) Latex output
     if np
diff --git a/matlab/dynare_identification.m b/matlab/dynare_identification.m
index 2a621b90fdd15318df6b1987886a1ce97532c499..b60ffec943a678d2afde8de6a598d8c695bacfdc 100644
--- a/matlab/dynare_identification.m
+++ b/matlab/dynare_identification.m
@@ -158,7 +158,7 @@ if ~(exist('sylvester3mr','file')==2),
     addpath([dynareroot 'gensylv'])
 end
 
-IdentifDirectoryName = CheckPath('identification');
+IdentifDirectoryName = CheckPath('identification',M_.dname);
 if prior_exist,
 
     indx = [];
diff --git a/matlab/dynare_sensitivity.m b/matlab/dynare_sensitivity.m
index 691a953d9f8187eebdfed091f9e2f60df813a062..a832a87afc9c77e1b1f90992154fd4622d140bf2 100644
--- a/matlab/dynare_sensitivity.m
+++ b/matlab/dynare_sensitivity.m
@@ -136,12 +136,12 @@ if options_gsa.morris==1 || options_gsa.morris==3,
     options_gsa.ksstat=1;
     if options_gsa.morris==3,
         options_gsa = set_default_option(options_gsa,'Nsam',256);
-        OutputDirectoryName = CheckPath('gsa/identif');
+        OutputDirectoryName = CheckPath('gsa/identif',M_.dname);
     else
-        OutputDirectoryName = CheckPath('gsa/screen');
+        OutputDirectoryName = CheckPath('gsa/screen',M_.dname);
     end
 else
-    OutputDirectoryName = CheckPath('gsa');
+    OutputDirectoryName = CheckPath('gsa',M_.dname);
 end
 
 options_.opt_gsa = options_gsa;
@@ -281,7 +281,7 @@ if options_gsa.glue,
     dr_ = oo_.dr;
     if options_gsa.ppost
         load([OutputDirectoryName,'/',fname_,'_post']);
-        DirectoryName = CheckPath('metropolis');
+        DirectoryName = CheckPath('metropolis',M_.dname);
     else
         if options_gsa.pprior
             load([OutputDirectoryName,'/',fname_,'_prior']);
diff --git a/matlab/get_name_of_the_last_mh_file.m b/matlab/get_name_of_the_last_mh_file.m
index 77368a5de774fe4fd7a6f8ca2fb3ad646a885e25..c3f39e6a6477d506e01bfe810947c142bac10fc9 100644
--- a/matlab/get_name_of_the_last_mh_file.m
+++ b/matlab/get_name_of_the_last_mh_file.m
@@ -31,7 +31,7 @@ function [mhname,info] = get_name_of_the_last_mh_file(M_)
 mhname = [];
 info = 1;
 
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 
 load([ MhDirectoryName '/' M_.fname '_mh_history.mat']) ;
 mh_number = record.LastFileNumber ;
diff --git a/matlab/gsa/filt_mc_.m b/matlab/gsa/filt_mc_.m
index c1e5650b9ca13f8f7e5e4b263c9f43e4a49d079d..5fb84cfbff812aa7656f6884653d00bc67b3c912 100644
--- a/matlab/gsa/filt_mc_.m
+++ b/matlab/gsa/filt_mc_.m
@@ -93,14 +93,14 @@ end
 
 if options_.opt_gsa.ppost,
     fnamtmp=[fname_,'_post'];
-    DirectoryName = CheckPath('metropolis');
+    DirectoryName = CheckPath('metropolis',M_.dname);
 else
     if options_.opt_gsa.pprior
         fnamtmp=[fname_,'_prior'];
-        DirectoryName = CheckPath(['gsa' filesep 'prior']);
+        DirectoryName = CheckPath(['gsa' filesep 'prior'],M_.dname);
     else
         fnamtmp=[fname_,'_mc'];
-        DirectoryName = CheckPath(['gsa' filesep 'mc']);
+        DirectoryName = CheckPath(['gsa' filesep 'mc'],M_.dname);
     end
 end
 if ~loadSA,
diff --git a/matlab/gsa/gsa_plotmatrix.m b/matlab/gsa/gsa_plotmatrix.m
index dfbd8790e3ee60463b636cf1e6779c26cbde4b55..1309e875a2da5f039f1e8d7d47588b443bf39486 100644
--- a/matlab/gsa/gsa_plotmatrix.m
+++ b/matlab/gsa/gsa_plotmatrix.m
@@ -21,7 +21,7 @@ function gsa_plotmatrix(type,varargin)
 
 global bayestopt_ options_ M_
 
-RootDirectoryName = CheckPath('gsa');
+RootDirectoryName = CheckPath('gsa',M_.dname);
 
 if options_.opt_gsa.pprior
     load([ RootDirectoryName filesep  M_.fname '_prior.mat'],'lpmat0','lpmat','istable','iunstable','iindeterm','iwrong')
diff --git a/matlab/ident_bruteforce.m b/matlab/ident_bruteforce.m
index 4be84c1c6499e50ed49c7510f2633dbc523cc811..93b7a5c58f5f6a997e621ef5691eff29bf564708 100644
--- a/matlab/ident_bruteforce.m
+++ b/matlab/ident_bruteforce.m
@@ -27,7 +27,7 @@ function [pars, cosnJ] = ident_bruteforce(J,n,TeX, pnames_TeX)
 % along with Dynare.  If not, see <http://www.gnu.org/licen
 global M_ options_
 
-OutputDirectoryName = CheckPath('Identification');
+OutputDirectoryName = CheckPath('Identification',M_.dname);
 
 k = size(J,2); % number of parameters
 
diff --git a/matlab/independent_metropolis_hastings_core.m b/matlab/independent_metropolis_hastings_core.m
index f4aecb46961473c983c4fe73abcf6172ee733f67..af62a5338e4c6e570bb41ddb3d2180dbfe6ac915 100644
--- a/matlab/independent_metropolis_hastings_core.m
+++ b/matlab/independent_metropolis_hastings_core.m
@@ -71,7 +71,7 @@ end
 % (re)Set the penalty.
 bayestopt_.penalty = Inf;
 
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 
 OpenOldFile = ones(nblck,1);
 if strcmpi(ProposalFun,'rand_multivariate_normal')
diff --git a/matlab/marginal_density.m b/matlab/marginal_density.m
index e423ea807deb68f807ba2349815105a450bbe17c..2bb23e737267e1d9da04ce0c563671d660019103 100644
--- a/matlab/marginal_density.m
+++ b/matlab/marginal_density.m
@@ -36,7 +36,7 @@ function [marginal,oo_] = marginal_density(M_, options_, estim_params_, oo_)
 npar = estim_params_.np+estim_params_.nvn+estim_params_.ncx+estim_params_.ncn+estim_params_.nvx;
 nblck = options_.mh_nblck;
 
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 load([ MhDirectoryName '/'  M_.fname '_mh_history.mat'])
 
 FirstMhFile = record.KeepedDraws.FirstMhFile;
diff --git a/matlab/metropolis_draw.m b/matlab/metropolis_draw.m
index eb8e8e150f92914d74a5b059910bef90b2241718..86fce488ec55bd25903fd2ef0375a15776d0f197 100644
--- a/matlab/metropolis_draw.m
+++ b/matlab/metropolis_draw.m
@@ -39,7 +39,7 @@ if init
     ncn  = estim_params_.ncn;
     np   = estim_params_.np ;
     npar = nvx+nvn+ncx+ncn+np;
-    MhDirectoryName = CheckPath('metropolis');
+    MhDirectoryName = CheckPath('metropolis',M_.dname);
     fname = [ MhDirectoryName '/' M_.fname];
     load([ fname '_mh_history']);
     FirstMhFile = record.KeepedDraws.FirstMhFile;
diff --git a/matlab/metropolis_hastings_initialization.m b/matlab/metropolis_hastings_initialization.m
index 9ddcd2f797b42cb1a989905279a803593d1fd62e..80c2f3532a809cd39a3c376ab98e134a5775a37c 100644
--- a/matlab/metropolis_hastings_initialization.m
+++ b/matlab/metropolis_hastings_initialization.m
@@ -56,7 +56,7 @@ end
 
 bayestopt_.penalty = 1e8;
 
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 
 nblck = options_.mh_nblck;
 nruns = ones(nblck,1)*options_.mh_replic;
diff --git a/matlab/mh_autocorrelation_function.m b/matlab/mh_autocorrelation_function.m
index 446a567010b4e0a83d744cb720de2871c6465fa6..a77506578b9ddaf3a5c73155c03c94af2f87eb2c 100644
--- a/matlab/mh_autocorrelation_function.m
+++ b/matlab/mh_autocorrelation_function.m
@@ -47,7 +47,7 @@ if isempty(column)
 end
 
 % Get informations about the posterior draws:
-DirectoryName = CheckPath('metropolis');
+DirectoryName = CheckPath('metropolis',M_.dname);
 try
     load([DirectoryName '/' M_.fname '_mh_history.mat']); 
 catch
diff --git a/matlab/pm3_core.m b/matlab/pm3_core.m
index 11090b9416d270f4d8e6d2429cf28a0d1f7391fd..7cfb3f6cbeaa5a7b79d1950e369f3f9864605a05 100644
--- a/matlab/pm3_core.m
+++ b/matlab/pm3_core.m
@@ -96,7 +96,7 @@ for i=fpar:nvar
     
     if whoiam,
         if Parallel(ThisMatlab).Local==0
-            DirectoryName = CheckPath('Output');
+            DirectoryName = CheckPath('Output',M_.dname);
         end
     end
     
diff --git a/matlab/prior_posterior_statistics.m b/matlab/prior_posterior_statistics.m
index 6ae0a3e55a3c6a658267340f360cd74de176a263..9c6cffcf0dcd1e59a0dc8e226b0f17fb4935c92c 100644
--- a/matlab/prior_posterior_statistics.m
+++ b/matlab/prior_posterior_statistics.m
@@ -69,7 +69,7 @@ end
 maxlag = M_.maximum_endo_lag;
 %%
 if strcmpi(type,'posterior')
-    DirectoryName = CheckPath('metropolis');
+    DirectoryName = CheckPath('metropolis',M_.dname);
     load([ DirectoryName '/'  M_.fname '_mh_history'])
     FirstMhFile = record.KeepedDraws.FirstMhFile;
     FirstLine = record.KeepedDraws.FirstLine;
@@ -86,12 +86,12 @@ if strcmpi(type,'posterior')
         B = min(1200, round(0.25*NumberOfDraws));
     end
 elseif strcmpi(type,'gsa')
-    RootDirectoryName = CheckPath('gsa');
+    RootDirectoryName = CheckPath('gsa',M_.dname);
     if options_.opt_gsa.pprior
-        DirectoryName = CheckPath(['gsa',filesep,'prior']);
+        DirectoryName = CheckPath(['gsa',filesep,'prior'],M_.dname);
         load([ RootDirectoryName filesep  M_.fname '_prior.mat'],'lpmat0','lpmat','istable')
     else
-        DirectoryName = CheckPath(['gsa',filesep,'mc']);
+        DirectoryName = CheckPath(['gsa',filesep,'mc'],M_.dname);
         load([ RootDirectoryName filesep  M_.fname '_mc.mat'],'lpmat0','lpmat','istable')
     end
     x=[lpmat0(istable,:) lpmat(istable,:)];
@@ -99,7 +99,7 @@ elseif strcmpi(type,'gsa')
     NumberOfDraws=size(x,1);
     B=NumberOfDraws; 
 elseif strcmpi(type,'prior')
-    DirectoryName = CheckPath('prior');
+    DirectoryName = CheckPath('prior',M_.dname);
     if ~isempty(options_.subdraws)
         B = options_.subdraws;
     else
diff --git a/matlab/prior_posterior_statistics_core.m b/matlab/prior_posterior_statistics_core.m
index 1c3349e4f362c26b801e613a81bcba074b278c12..7787829dbbeeedaff9ed6e47d61f9400b05cb99f 100644
--- a/matlab/prior_posterior_statistics_core.m
+++ b/matlab/prior_posterior_statistics_core.m
@@ -97,15 +97,15 @@ end
 
 % DirectoryName = myinputs.DirectoryName;
 if strcmpi(type,'posterior')
-    DirectoryName = CheckPath('metropolis');
+    DirectoryName = CheckPath('metropolis',M_.dname);
 elseif strcmpi(type,'gsa')
     if options_.opt_gsa.pprior
-        DirectoryName = CheckPath(['gsa',filesep,'prior']);
+        DirectoryName = CheckPath(['gsa',filesep,'prior'],M_.dname);
     else
-        DirectoryName = CheckPath(['gsa',filesep,'mc']);
+        DirectoryName = CheckPath(['gsa',filesep,'mc'],M_.dname);
     end
 elseif strcmpi(type,'prior')
-    DirectoryName = CheckPath('prior');
+    DirectoryName = CheckPath('prior',M_.dname);
 end
 
 RemoteFlag = 0;
diff --git a/matlab/prior_sampler.m b/matlab/prior_sampler.m
index 36ba42bdd5d53581e1831e3c0a449e930d98b8eb..ff7c1e8cff5b2d6859c4da2a8fc35d5d3949ada9 100644
--- a/matlab/prior_sampler.m
+++ b/matlab/prior_sampler.m
@@ -32,7 +32,7 @@ function results = prior_sampler(drsave,M_,bayestopt_,options_,oo_)
 
 % Initialization.
 prior_draw(1);
-PriorDirectoryName = CheckPath('prior/draws');
+PriorDirectoryName = CheckPath('prior/draws',M_.dname);
 work = ~drsave;
 iteration = 0;
 loop_indx = 0;
diff --git a/matlab/random_walk_metropolis_hastings_core.m b/matlab/random_walk_metropolis_hastings_core.m
index 398984b549cad663946700932ac73b12c83371ec..1898d478ab48dca812c639a50c11bc3ba09a7de1 100644
--- a/matlab/random_walk_metropolis_hastings_core.m
+++ b/matlab/random_walk_metropolis_hastings_core.m
@@ -103,7 +103,7 @@ end
 % (re)Set the penalty
 bayestopt_.penalty = Inf;
 
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 
 options_.lik_algo = 1;
 OpenOldFile = ones(nblck,1);
diff --git a/matlab/selec_posterior_draws.m b/matlab/selec_posterior_draws.m
index d1b60fb389d655988bc55f48f6049926196cb9cc..59f0fe07a7ae8df83f1563fbc3f4f54b0a669d86 100644
--- a/matlab/selec_posterior_draws.m
+++ b/matlab/selec_posterior_draws.m
@@ -63,7 +63,7 @@ switch nargin
 end
 
 % Get informations about the mcmc:
-MhDirectoryName = CheckPath('metropolis');
+MhDirectoryName = CheckPath('metropolis',M_.dname);
 fname = [ MhDirectoryName '/' M_.fname];
 load([ fname '_mh_history.mat']);
 FirstMhFile = record.KeepedDraws.FirstMhFile;
diff --git a/matlab/set_prior.m b/matlab/set_prior.m
index ffc1bdaa81ce0499c43804a57cbe855d5311cbd7..2d3f4534448dc62d2c6c6b98564ccfcdee9f2c10 100644
--- a/matlab/set_prior.m
+++ b/matlab/set_prior.m
@@ -263,7 +263,7 @@ if options_.initialize_estimated_parameters_with_the_prior_mode
 end 
 
 % I create subfolder M_.dname/prior if needed.
-CheckPath('prior');
+CheckPath('prior',M_.dname);
 
 % I save the prior definition if the prior has changed.
 if exist([ M_.dname '/prior/definition.mat'])
diff --git a/matlab/trace_plot.m b/matlab/trace_plot.m
index fb8d6f5f9553bfad006b61b8ef6d1ac52b9e804f..588eadb91a60680b6509648ba9eb0dd57844316c 100644
--- a/matlab/trace_plot.m
+++ b/matlab/trace_plot.m
@@ -46,7 +46,7 @@ if isempty(column)
 end
 
 % Get informations about the posterior draws:
-DirectoryName = CheckPath('metropolis');
+DirectoryName = CheckPath('metropolis',M_.dname);
 try
     load([DirectoryName '/' M_.fname '_mh_history.mat']); 
 catch