diff --git a/matlab/histvalf_initvalf.m b/matlab/histvalf_initvalf.m
index f6032a7bf3a44dde19b55afa43aac6c2d0057176..5723d997960808d5e8704da5de5ce9bc865f3f26 100644
--- a/matlab/histvalf_initvalf.m
+++ b/matlab/histvalf_initvalf.m
@@ -133,7 +133,7 @@ last_obs_ispresent = false;
 first_obs = periods(1);
 if isfield(options, 'first_obs') && ~isempty(options.first_obs)
     if options.first_obs < 1
-        error('first_obs must be a positive number')
+        error([caller, '_FILE: first_obs must be a positive number'])
     elseif options.first_obs > nobs0
         error(sprintf([caller, '_FILE: first_obs = %d is larger than the number', ...
                        ' of observations in the data file (%d)'], ...
@@ -146,7 +146,7 @@ if isfield(options, 'first_obs') && ~isempty(options.first_obs)
             error(sprintf([caller, '_FILE: first_obs = %d and', ...
                            ' first_simulation_period = %d have values', ...
                            ' inconsistent with a maximum lag of %d periods'], ...
-                          options.first_obs, options_.first_simulation_period, ...
+                          options.first_obs, options.first_simulation_period, ...
                           M.orig_maximum_lag))
         end
     elseif isfield(options, 'firstsimulationperiod')
@@ -157,11 +157,11 @@ if isfield(options, 'first_obs') && ~isempty(options.first_obs)
             error(sprintf([caller, '_FILE: first_obs = %d and', ...
                            ' first_simulation_period = %s have values', ...
                            ' inconsistent with a maximum lag of %d periods'], ...
-                          options.first_obs, options_.firstsimulationperiod, ...
+                          options.first_obs, options.firstsimulationperiod, ...
                           M.orig_maximum_lag))
         end
     else
-        first_obs = periods(options_.first_obs);
+        first_obs = periods(options.first_obs);
     end
     first_obs_ispresent = true;
 end
@@ -172,10 +172,10 @@ if isfield(options, 'firstobs') && ~isempty(options.firstobs)
                 - M.orig_maximum_lag
             first_obs = options.firstobs;
         else
-            error(sprintf([caller, '_File: first_obs = %s and', ...
+            error(sprintf([caller, '_FILE: first_obs = %s and', ...
                            ' first_simulation_period = %d have values', ...
                            ' inconsistent with a maximum lag of %d periods'], ...
-                          options.firstobs, options_.first_simulation_period, ...
+                          options.firstobs, options.first_simulation_period, ...
                           M.orig_maximum_lag))
         end
     elseif isfield(options, 'firstsimulationperiod')
@@ -220,8 +220,8 @@ end
 
 if isfield(options, 'last_obs')
     if options.last_obs > nobs0
-        error(sprintf([caller, '_FILE: last_obs is larger than the number', ...
-                       'observations in the dataset (%d)'], ...
+        error(sprintf([caller, '_FILE: last_obs = %d is larger than the number', ...
+                       ' of observations in the dataset (%d)'], ...
                       options.last_obs, nobs0))
     elseif first_obs_ispresent
         if nobs > 0 && (periods(options.last_obs) ~= first_obs + nobs - 1)
@@ -229,13 +229,20 @@ if isfield(options, 'last_obs')
                    ' inconsistent information. Use only two of these', ...
                    ' options.'])
         else
-            last_obs = periods(options.last_obs)
+            last_obs = periods(options.last_obs);
+        end
+    else
+        last_obs = periods(options.last_obs);
+        if nobs > 0
+            first_obs = last_obs - nobs + 1;
+        else
+            first_obs = periods(1);
         end
     end
 elseif isfield(options, 'lastobs')
     if options.lastobs > series.last
         error(sprintf([caller, '_FILE: last_obs = %s is larger than the number', ...
-                       'observations in the dataset (%s)'], ...
+                       ' of observations in the dataset (%s)'], ...
                       options.lastobs, series.last))
     elseif first_obs_ispresent
         if nobs > 0 && (options.lastobs ~= first_obs + nobs - 1)
@@ -243,13 +250,20 @@ elseif isfield(options, 'lastobs')
                    ' inconsistent information. Use only two of these', ...
                    ' options.'])
         else
-            last_obs = options.last_obs
+            last_obs = options.last_obs;
+        end
+    else
+        last_obs = options.last_obs;
+        if nobs > 0
+            first_obs = last_obs - nobs + 1;
+        else
+            first_obs = periods(1);
         end
     end
 elseif nobs > 0
-    last_obs = first_obs + nobs - 1
+    last_obs = first_obs + nobs - 1;
 else
-    last_obs = series.last
+    last_obs = series.last;
 end
 
 series = series(first_obs:last_obs);
diff --git a/tests/histval_initval_file/my_assert.m b/tests/histval_initval_file/my_assert.m
index 2ac132e12d72b0c349f2a35cfd8666bf6f3a11a9..6f30d2dbe25569c41edc26d6efe47c81ed29daea 100644
--- a/tests/histval_initval_file/my_assert.m
+++ b/tests/histval_initval_file/my_assert.m
@@ -1,4 +1,4 @@
 function failed_tests = my_assert(failed_tests, success, test_name)
 if ~success
-    failed_tests = cat(1, test_failed, test_name);
+    failed_tests = cat(1, failed_tests, test_name);
 end
\ No newline at end of file
diff --git a/tests/histval_initval_file_unit_tests.m b/tests/histval_initval_file_unit_tests.m
index 4b81f71de5588fd15d26a2a42b4cf78f6d1aaa52..925ce1a571503cdf07602ae039de6ec1a95dd26f 100644
--- a/tests/histval_initval_file_unit_tests.m
+++ b/tests/histval_initval_file_unit_tests.m
@@ -16,6 +16,7 @@ M.endo_names = {'Variable_1','Variable_2','Variable_3'};
 M.exo_nbr = 1;
 M.exo_names = {'Variable_4'};
 M.exo_det_nbr = 0;
+M.orig_maximum_lag = 2;
 
 caller = 'INITVAL';
 
@@ -88,9 +89,9 @@ try
     ds1 = histvalf_initvalf(caller, M, options);
     error('This test didn''t catch the error')
 catch me
-    if strcmp(me.message, ['INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
-                           ' inconsistent information. Use only two of these', ...
-                           ' options.']) == false
+    if ~strcmp(me.message, strcat('INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
+                                  ' inconsistent information. Use only two of these', ...
+                                  ' options.'))
         failed_tests = cat(1, failed_tests, 'Wrong nobs error message' );
     end
 end
@@ -98,14 +99,13 @@ num_tests = num_tests + 1;
 
 options = struct();
 options.series = ds;
-options.first_obs = -1;
+options.first_obs = 0;
 
 try
     ds1 = histvalf_initvalf(caller, M, options);
     error('This test didn''t catch the error')
 catch me
-    if strcmp(me.message, [caller, '_FILE: the first requested period is', ...
-                           ' before available data.']) == false
+    if ~strcmp(me.message, strcat(caller, '_FILE: first_obs must be a positive number'))
         failed_tests = cat(1, failed_tests, ...
                            'Wrong first period error message');
     end
@@ -120,8 +120,8 @@ try
     ds1 = histvalf_initvalf(caller, M, options);
     error('This test didn''t catch the error')
 catch me
-    if strcmp(me.message, [caller, '_FILE: the last requested period is', ...
-                           ' after available data.']) == false
+    if ~strcmp(me.message, strcat(caller, '_FILE: last_obs = 11 is larger than the number', ...
+                                  ' of observations in the dataset (10)'))
         failed_tests = cat(1, failed_tests, ...
                            'Wrong last period error message');
     end