function stack = TiffStackImport(filename)

max_num_slices = 2000;
% Make an assumption about the highest number of slices the program is
% likely to see.  If higher than this can explain the problem to the user.

toggler =0 ;

tiff_stack = Tiff([filename '.tiff']);

j = 1;
while j <= max_num_slices && ~toggler
    
    if tiff_stack.lastDirectory == 0
        tiff_stack.nextDirectory;
    elseif tiff_stack.lastDirectory ==1
        last_directory = tiff_stack.currentDirectory;
        toggler =1;
    end
    
    j = j +1;
end

if toggler == 1
    
test_im = tiff_stack.read;
img_width = size(test_im,2);
img_height = size(test_im,1);
stack = zeros(img_height,img_width,last_directory);
tiff_stack.setDirectory(1);

    for j = 1:last_directory
        
%         if (j>=lower_cutoff)&& (j<=upper_cutoff)
        stack(:,:, j) = tiff_stack.read;
        
        % there was at one point a line here that read stack(:,:, j) = 5*tiff_stack.read;
        % apparently not necessary if you use fractional values (e.g. .5)
        % for threshold in starrynite params.
%         end
        
        if tiff_stack.lastDirectory ~= 1
        tiff_stack.nextDirectory
        end
                
    end
    
    tiff_stack.close;
    
else
    fprintf('Stack larger than max size.  Increase max_num_slices to read in stack.')
end

end