diff --git a/crpptbx/gdat.m b/crpptbx/gdat.m
index a02c2f625eac1be8ebe0f6a5c9e1fb6cbc079812..2f331445e896e4206e52bcae96df66d87ea466c0 100644
--- a/crpptbx/gdat.m
+++ b/crpptbx/gdat.m
@@ -118,15 +118,12 @@ end
 
 % Avoid running addpath over and over, this can take some time when there
 % are many folders in the path
-persistent last_machine
-
-if isempty(last_machine) || ~strcmpi(last_machine,machine_eff)
-  % add paths to each machine which are in subdirectory of gdat and working
-  % add only given machine directory using machine_eff...
-  gdat_path = mfilename('fullpath');
-  addpath([gdat_path(1:end-4) upper(machine_eff)]);
-  last_machine = machine_eff;
+subfunction = sprintf('gdat_%s',deblank(lower(machine_eff)));
+if isempty(which(subfunction)),
+  gdat_path = fileparts(mfilename('fullpath'));
+  addpath(fullfile(gdat_path,upper(machine_eff)));
 end
+% NOTE: we could also check if it matches the path to the main function.
     
 % copy gdat present call:
 gdat_call = [];
diff --git a/crpptbx/gdatpaths.m b/crpptbx/gdatpaths.m
index 57e41db3f255c08c8fc8b908c6e85b243965cc50..9313eba007d4f577a5ee1266fe7c79fcbc57cb60 100644
--- a/crpptbx/gdatpaths.m
+++ b/crpptbx/gdatpaths.m
@@ -1,11 +1,12 @@
+function gdatpaths
 %
 % add paths for gdat directory
 %
 % assumes gdat already available
 %
 a=which('gdat');
-ii=findstr('/',a);
-a=a(1:ii(end));
+if isempty(a), error('gdat is not in the MATLAB path'); end
+a = fileparts(a);
 
 machines=[{'JET'} {'TCV'} {'AUG'} {'D3D'}];
 machines=[{'JET'} {'TCV'} {'AUG'} {'KSTAR'}];
@@ -13,7 +14,6 @@ machines=[{'JET'} {'TCV'} {'AUG'} {'D3D'} {'KSTAR'}];
 
 add_paths = cell(1,length(machines));
 for i=1:length(machines)
-  add_paths{i}=[a machines{i}];
+  add_paths{i}=fullfile(a,machines{i});
 end
 addpath(add_paths{:});
-