function [complete_cross_transform_corrected,last_score,moving_nuclei_seg,moving_nuclei_centers] = RegisterByNucleiRefineCross(optreg,calculated_params,this_moving_stack_number,this_target_stack_number,moving_moves,target_targeted,do_theta,theta_number...
    ,do_pre_cross_transform,write_files,moving_nuclei_seg,moving_nuclei_centers)


nfigs= get(0,'Children');
if size(nfigs,1)>50
    close all
end

color = 0;

prefix = optreg.input_prefix;
num_segmentation_variables = optreg.num_segmentation_variables;
img_width = optreg.img_width;
img_height = optreg.img_height;
num_slices = optreg.num_slices;
new_num_slices = optreg.new_frame(3);
z_multiplier = optreg.z_multiplier;
center_position = optreg.center_position;
new_frame = optreg.new_frame;

background_variable = optreg.background_segmentation_variable;
nuclei_variable = optreg.nuclei_segmentation_variable;
emb_outline_variable = optreg.emb_background_segmentation_variable;
cell_body_variable = optreg.cell_body_segmentation_variable;
neurite_variable = optreg.neurite_segmentation_variable;

do_emb_background_reg = optreg.do_emb_background_reg;
second_round_nuclear = optreg.second_round_nuclear;

if do_emb_background_reg
emb_background_2D = optreg.emb_background_2D;
emb_background_fix_centroid = optreg.emb_background_fix_centroid;
end

% nuclei_2D = optreg.nuclei_2D;
nuclei_2D = false;
nuclei_fix_centroid = optreg.nuclei_fix_centroid;

use_h5 = optreg.use_h5;

start_time = optreg.start_time;
save_intermediate_images = optreg.save_intermediate_images;
save_intermediate_images_head = optreg.save_intermediate_images_head;


if moving_moves
    this_moving_dataset_directory = optreg.moving_dataset_directory;
    this_moving_dataset = optreg.moving_dataset;
    this_moving_position = optreg.moving_position;
    this_moving_moments_time = optreg.moving_moments_time;
    this_moving_run_number = optreg.moving_run_number;
    this_moving_strain_name = optreg.moving_strain_name;
else
    this_moving_dataset_directory = optreg.target_dataset_directory;
    this_moving_dataset = optreg.target_dataset;
    this_moving_position = optreg.target_position;
    this_moving_moments_time = optreg.target_moments_time;
    this_moving_run_number = optreg.target_run_number;
    this_moving_strain_name = optreg.target_strain_name;
end

start_time = optreg.start_time;
target_start_time = optreg.target_start_time;

if ~target_targeted
    this_target_dataset_directory = optreg.moving_dataset_directory;
    this_target_dataset = optreg.moving_dataset;
    this_target_position = optreg.moving_position;
    this_target_moments_time = optreg.moving_moments_time;
    this_target_run_number = optreg.moving_run_number;
    this_target_strain_name = optreg.moving_strain_name;
else
    this_target_dataset_directory = optreg.target_dataset_directory;
    this_target_dataset = optreg.target_dataset;
    this_target_position = optreg.target_position;
    this_target_moments_time = optreg.target_moments_time;
    this_target_run_number = optreg.target_run_number;
    this_target_strain_name = optreg.target_strain_name;
end

if moving_moves==target_targeted
    is_cross_reg = true
    save_intermediate_images = true;
    force_centroids_unfix = optreg.unfix_cross_reg_centroids;
else
    is_cross_reg = false
    force_centroids_unfix = false;
end


% num_segmentation_variables = 4;
% nuclei_variable = 2;

center = [center_position 1];

stretch_matrix = [1 0 0 0; 0 1 0 0; 0 0 z_multiplier 0; 0 0 0 1];

moving_filename = [prefix this_moving_strain_name 'Ds' num2str(this_moving_dataset) '_Pos' num2str(this_moving_position) '_Co' num2str(color) '_St' num2str(this_moving_stack_number)]
target_filename = [prefix this_target_strain_name 'Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co' num2str(color) '_St' num2str(this_target_stack_number)]

nothing = 0;


cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number)])
if ~do_theta
    if moving_moves
        load(['moving transform matrix_' num2str(this_moving_strain_name) num2str(this_moving_dataset) '_' num2str(this_moving_position) '_r' num2str(this_moving_run_number) '. ' this_moving_moments_time '.mat'])
    else
        load(['target transform matrix_' num2str(this_target_strain_name) num2str(this_moving_dataset) '_' num2str(this_moving_position) '_r' num2str(this_moving_run_number) '. ' this_moving_moments_time '.mat'])
    end
elseif do_theta
    load(['moving transform matrix_' num2str(this_moving_strain_name) num2str(this_moving_dataset) '_' num2str(this_moving_position) '_thetanumber_' num2str(theta_number) '_r' num2str(this_moving_run_number) '. ' this_moving_moments_time '.mat']);
end
transform_matrix_moving = transform_matrix;



if isequal(this_moving_dataset_directory,this_target_dataset_directory)&&isequal(moving_filename,target_filename)
    cd(this_moving_dataset_directory)
    moving_stack_raw = TiffStackImport(moving_filename);
    cpd_transform_conjugate = eye(4);
    sigma2 = .0000000000001;
    cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfTransforms'])
    save(['cpd transform conjugate_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
    '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'cpd_transform_conjugate');
    save(['sigma2_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)... 
    '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'sigma2')
    
    if save_intermediate_images
    img_output = imwarp(moving_stack_raw,affine3d(stretch_matrix*transform_matrix_moving.'),'OutputView',imref3d(new_frame));
    cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfRegIms'])
    TiffStackWrite(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
    '_r' num2str(this_moving_run_number) '. ' start_time],16);
    end

    if save_intermediate_images_head
        
    on_head_matrix = [0 0 1 0; 0 1 0 0; -1 0 0 0; 0 0 0 1];
    first_translation = [-center_position 1].';
    first_translation = [[eye(3);[0 0 0]] first_translation];
    second_translation = [center_position 1].';
    second_translation = [[eye(3);[0 0 0]] second_translation];
    on_head_transform = first_translation.'*on_head_matrix*second_translation.';

    img_output2 = imwarp(moving_stack_raw,affine3d(stretch_matrix*transform_matrix_moving.'*on_head_transform),'OutputView',imref3d(new_frame));
    
    cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfRegIms'])
    TiffStackWrite(img_output2,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
    '_r' num2str(this_moving_run_number) '. on head ' start_time],16);

    end
    
    return
end

scaling_factor = 1;
% Placeholder.  Can set a different scaling factor (e.g. using the scaling
% in cpd register).

cd([this_target_dataset_directory '/Run ' num2str(this_target_run_number)])
if target_targeted
    load(['target transform matrix_' num2str(this_target_dataset) '_' num2str(this_target_position) '_r' num2str(this_target_run_number) '. ' this_target_moments_time '.mat'])
else
    load(['moving transform matrix_' num2str(this_target_dataset) '_' num2str(this_target_position) '_r' num2str(this_target_run_number) '. ' this_target_moments_time '.mat'])
end
transform_matrix_target = transform_matrix;

cd ..

cd(this_moving_dataset_directory)

% Options for cpd_register (atm, not saved in optreg, because not changing):
opt.method = 'rigid';
opt.scale = 0;
opt.normalize = optreg.cpd_normalize;
opt.viz = 1;
opt.fgt = 0;
opt.corresp = 0;

% Holdover from earlier version of the program which registered on emb
% outline channel prior to nuclear registration. Not tested recently.
if do_emb_background_reg
target_emb_background = squeeze(target_prob_array(:,:,:,emb_outline_variable)) + squeeze(target_prob_array(:,:,:,nuclei_variable)) + squeeze(target_prob_array(:,:,:,cell_body_variable));
moving_emb_background = squeeze(moving_prob_array(:,:,:,emb_outline_variable)) + squeeze(moving_prob_array(:,:,:,nuclei_variable)) + squeeze(moving_prob_array(:,:,:,cell_body_variable));

target_embryo_background_seg = target_emb_background>.5;
moving_embryo_background_seg = moving_emb_background>.5;

num_points_target_emb_background = nnz(target_embryo_background_seg);

points_target_emb_background = zeros(num_points_target_emb_background,3);

counter =0;

for q = 1:img_height
    for j = 1:img_width
        for k = 1:new_num_slices
            if target_embryo_background_seg(q,j,k) == 1;
                counter = counter + 1;
                points_target_emb_background(counter,:) = [j q k];
            end
        end
    end
end

points_stretched_target_emb_background = points_target_emb_background * [1 0 0; 0 1 0; 0 0 3.077];

condensed_points_target_emb_background = zeros(5000,3);
for q = 1:5000
    condensed_points_target_emb_background(q,:) = points_stretched_target_emb_background(randi([1 num_points_target_emb_background],1),:);
end

rotated_points_target_emb_background = [condensed_points_target_emb_background ones(5000,1)]*transform_matrix_target.';

num_points_moving_emb_background = nnz(moving_embryo_background_seg);
points_moving_emb_background = zeros(num_points_moving_emb_background,3);

counter =0;

for q = 1:img_height
    for j = 1:img_width
        for k = 1:new_num_slices
            if moving_embryo_background_seg(q,j,k) == 1;
            counter = counter + 1;
                points_moving_emb_background(counter,:) = [j q k];
            end
        end
    end
end

points_stretched_moving_emb_background = points_moving_emb_background * [1 0 0; 0 1 0; 0 0 3.077];

condensed_points_moving_emb_background = zeros(5000,3);
for q = 1:5000
    condensed_points_moving_emb_background(q,:) = points_stretched_moving_emb_background(randi([1 num_points_moving_emb_background],1),:);
end

rotated_points_moving_emb_background = [condensed_points_moving_emb_background ones(5000,1)]*(transform_matrix_moving.');

points_moving_emb_background_transformed = (rotated_points_moving_emb_background*scaling_factor);

points_moving_emb_background_corrected = points_moving_emb_background_transformed;


    if emb_background_2D
        [first_cpd,c,sigma2_1] = cpd_register_insleyshaham_alt(rotated_points_target_emb_background(:,2:3),points_moving_emb_background_corrected(:,2:3),opt);
        %     scaling_factor = first_cpd.s;
        cpd_transform = [0 0 0;[first_cpd.R*scaling_factor first_cpd.t]];
        cpd_transform = [cpd_transform;[0 0 1]];
        cpd_transform = [[1;0;0;0] cpd_transform];
    else
        [first_cpd,c,sigm2_1] = cpd_register_insleyshaham_alt(rotated_points_target_emb_background(:,1:3),points_moving_emb_background_corrected(:,1:3),opt);
        %     scaling_factor = first_cpd.s;
        cpd_transform = [[first_cpd.R*scaling_factor first_cpd.t]; [0 0 0 1]];
    end
    if emb_background_fix_centroid && ~force_centroids_unfix
        new_center = center*cpd_transform.'
        additional_t = center-new_center
        cpd_transform = cpd_transform + [0 0 0 0; 0 0 0 0; 0 0 0 0; additional_t].';
        fprintf('additional_t applied \r')
    else
        new_center = center*cpd_transform.'
        additional_t = center-new_center
        fprintf('additional_t not applied \r')
    end
elseif ~do_emb_background_reg
    cpd_transform = eye(4);
    sigma2_1 = .0000000000001;
end
    

% target_nuclei_seg = squeeze(target_prob_array(:,:,:,nuclei_variable)>.5);
% moving_nuclei_seg = squeeze(moving_prob_array(:,:,:,nuclei_variable)>.5);

fprintf('start getting nuclei')

if do_pre_cross_transform
    color = 0;
    last_target_filename = [prefix this_target_strain_name 'Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co' num2str(color) '_St' num2str(calculated_params.corrected_last_target_stack)];
    cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossTransforms'])
    ['complete cross transform_' moving_filename ' al ' last_target_filename '_rm' num2str(this_moving_run_number) '_rt' num2str(this_target_run_number) '. ' start_time '.mat']
    load(['complete cross transform_' moving_filename ' al ' last_target_filename '_rm' num2str(this_moving_run_number) '_rt' num2str(this_target_run_number) '. ' start_time '.mat'],'complete_cross_transform')
end

cd(this_moving_dataset_directory)

if isequal(moving_nuclei_seg,-1) && isequal(moving_nuclei_centers,-1)
[moving_nuclei_seg,moving_nuclei_centers] = GetNucleiObjectsRefineTest(optreg,calculated_params,this_moving_stack_number,moving_moves,complete_cross_transform,false,true);
end

figure; imshow3d(moving_nuclei_seg)

if do_pre_cross_transform
cd(this_target_dataset_directory)
cd(['Run ' num2str(this_target_run_number) '/SelfTransforms']);
    load(['composed cpd conjugate_' target_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(calculated_params.corrected_last_target_stack)...
        '_r' num2str(this_target_run_number) '. ' target_start_time '.mat'],'composed_cpd_conjugate')
else
    composed_cpd_conjugate = 0;
end

target_nuclei_seg = GetNucleiObjectsRefineRef(optreg,calculated_params,this_target_stack_number,~target_targeted,transform_matrix_target,false,true,moving_nuclei_centers,composed_cpd_conjugate);

% figure; imshow3d(target_nuclei_seg);
% figure; imshow3d(moving_nuclei_seg)

fprintf('stop getting nuclei');

num_points_target_nuclei = nnz(target_nuclei_seg);

points_target_nuclei = zeros(num_points_target_nuclei,3);

counter =0;

for q = 1:img_height
    for j = 1:img_width
        for k = 1:new_num_slices
            if  target_nuclei_seg(q,j,k) == 1;
                counter = counter + 1;
                points_target_nuclei(counter,:) = [j q k];
            end
        end
    end
end

num_condensed_points_target = 5000;

condensed_points_target_nuclei = zeros(num_condensed_points_target,3);
for q = 1:num_condensed_points_target
    condensed_points_target_nuclei(q,:) = points_target_nuclei(randi([1 num_points_target_nuclei],1),:);
end

num_points_moving_nuclei = nnz(moving_nuclei_seg);

points_moving_nuclei = zeros(num_points_moving_nuclei,3);

counter =0;

for q = 1:img_height
    for j = 1:img_width
        for k = 1:new_num_slices
            if moving_nuclei_seg(q,j,k) == 1;
            counter = counter + 1;
                points_moving_nuclei(counter,:) = [j q k];
            end
        end
    end
end

num_condensed_points_moving = 5000;

condensed_points_moving_nuclei = zeros(num_condensed_points_moving,3);
for q = 1:num_condensed_points_moving
    condensed_points_moving_nuclei(q,:) = points_moving_nuclei(randi([1 num_points_moving_nuclei],1),:);
end

opt.method = 'rigid';
opt.scale = 0;
opt.normalize = 0;
opt.num_x_orig = num_condensed_points_target;
opt.num_y_orig = num_condensed_points_moving;
opt.corresp = 1;

if nuclei_2D
    [second_cpd,c,sigma2_2,max_score,min_score,last_score,ntol,scores,ntols,sigmas] = cpd_register_insleyshaham_alt(condensed_points_target_nuclei(:,2:3),condensed_points_moving_nuclei(:,2:3),opt,optreg);
    % scaling_factor = second_cpd.s;
%     fprintf('c is')
%     c(1:5000,:)
    cpd_transform2 = [0 0 0;[second_cpd.R*scaling_factor second_cpd.t]];
    cpd_transform2 = [cpd_transform2;[0 0 1]];
    cpd_transform2 = [[1;0;0;0] cpd_transform2];
else
    % scaling_factor = second_cpd.s;
    [second_cpd,c,sigma2_2,max_score,min_score,last_score,ntol,scores,ntols,sigmas,all_scores_fwd] = cpd_register_insleyshaham_alt(condensed_points_target_nuclei(:,1:3),condensed_points_moving_nuclei(:,1:3),opt,optreg);
%     fprintf('c is')
%     c(1:5000,:)
    cpd_transform2 = [[second_cpd.R*scaling_factor second_cpd.t]; [0 0 0 1]];
end

if nuclei_fix_centroid && ~force_centroids_unfix
    new_center = center*cpd_transform2.'
    additional_t = center-new_center
    cpd_transform2 = cpd_transform2 + [0 0 0 0; 0 0 0 0; 0 0 0 0; additional_t].';
    fprintf('additional_t applied \r')
else
    new_center = center*cpd_transform2.'
    additional_t = center-new_center
    fprintf('additional_t not applied \r')
end

if nuclei_2D && second_round_nuclear
points_moving_nuclei_transformed2 = points_moving_nuclei_transformed*cpd_transform2.';
[third_cpd,c,sigma2_3] = cpd_register_insleyshaham_alt(points_target_nuclei(:,[1,3]),points_moving_nuclei_transformed2(:,[1,3]),opt);

cpd_transform3 = [third_cpd.R(1,1) 0 third_cpd.R(1,2) third_cpd.t(1); 0 1 0 0; third_cpd.R(2,1) 0 third_cpd.R(2,2) third_cpd.t(2); 0 0 0 1];

if nuclei_fix_centroid && ~force_centroids_unfix
    new_center = center*cpd_transform3.'
    additional_t = center-new_center
    cpd_transform3 = cpd_transform3 + [0 0 0 0; 0 0 0 0; 0 0 0 0; additional_t].';
    fprintf('additional_t applied \r')
else
    new_center = center*cpd_transform3.'
    additional_t = center-new_center
    fprintf('additional_t not applied \r')
end
else
cpd_transform3 = eye(4);
sigma2_3 = .0000000000001;
end

cd(this_moving_dataset_directory)
moving_stack_raw = TiffStackImport(moving_filename);
stretch_matrix = [1 0 0 0; 0 1 0 0; 0 0 z_multiplier 0; 0 0 0 1];

if ~do_pre_cross_transform
    cpd_transform_conjugate = cpd_transform.'*cpd_transform2.'*cpd_transform3.';
    sigma2 = sigma2_1 + sigma2_2 + sigma2_3;
    
    if write_files
%         if save_intermediate_images
            img_output = imwarp(moving_stack_raw,affine3d(stretch_matrix*transform_matrix_moving.'*cpd_transform_conjugate),'OutputView',imref3d(new_frame));
            
            if isequal(this_moving_dataset_directory,this_target_dataset_directory)
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfRegIms'])
            else
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossRegIms'])
            end
            
            TiffStackWrite(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
                '_r' num2str(this_moving_run_number) '. ' start_time],16);
%         end
        
        
%         if save_intermediate_images_head
            on_head_matrix = [0 0 1 0; 0 1 0 0; -1 0 0 0; 0 0 0 1];
            first_translation = [-center_position 1].';
            first_translation = [[eye(3);[0 0 0]] first_translation];
            second_translation = [center_position 1].';
            second_translation = [[eye(3);[0 0 0]] second_translation];
            on_head_transform_conjugate = cpd_transform_conjugate*first_translation.'*on_head_matrix*second_translation.';
            img_output = imwarp(moving_stack_raw,affine3d(stretch_matrix*transform_matrix_moving.'*on_head_transform_conjugate),'OutputView',imref3d(new_frame));
            
            if isequal(this_moving_dataset_directory,this_target_dataset_directory)
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfRegIms'])
            else
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossRegIms'])
            end
            
            TiffStackWrite(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
                '_r' num2str(this_moving_run_number) '. on head ' start_time],16);
%         end
        
        
        if isequal(this_moving_dataset_directory,this_target_dataset_directory)
            cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfTransforms'])
        else
            cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossTransforms'])
        end
        
        save(['cpd transform conjugate_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'cpd_transform_conjugate')
        
        
        save(['sigma2_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'sigma2')
        
    end
    
else
    
    complete_cross_transform_corrected = complete_cross_transform*cpd_transform.'*cpd_transform2.'*cpd_transform3.';
    sigma2 = sigma2_1 + sigma2_2 + sigma2_3;

    mkdir([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossRegIms/corrected'])
    
    if write_files
%         if save_intermediate_images
            img_output = imwarp(moving_stack_raw,affine3d(complete_cross_transform_corrected),'OutputView',imref3d(new_frame));
            
            if isequal(this_moving_dataset_directory,this_target_dataset_directory)
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfRegIms/'])
            else
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossRegIms/corrected'])
            end
            
            TiffStackWrite(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
                '_r' num2str(this_moving_run_number) '. corrected ' start_time],16);
            
            TiffStackMaxProjectCallable(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
                '_r' num2str(this_moving_run_number) '. corrected max ' start_time])
%         end
        
        
%         if save_intermediate_images_head
            on_head_matrix = [0 0 1 0; 0 1 0 0; -1 0 0 0; 0 0 0 1];
            first_translation = [-center_position 1].';
            first_translation = [[eye(3);[0 0 0]] first_translation];
            second_translation = [center_position 1].';
            second_translation = [[eye(3);[0 0 0]] second_translation];
            on_head_transform_conjugate = complete_cross_transform_corrected*first_translation.'*on_head_matrix*second_translation.';
            img_output = imwarp(moving_stack_raw,affine3d(on_head_transform_conjugate),'OutputView',imref3d(new_frame));
            
            if isequal(this_moving_dataset_directory,this_target_dataset_directory)
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/SelfRegIms'])
            else
                cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossRegIms/corrected'])
            end
            
            TiffStackWrite(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
                '_r' num2str(this_moving_run_number) '. on head corrected ' start_time],16);
            
            TiffStackMaxProjectCallable(img_output,[moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
                '_r' num2str(this_moving_run_number) '. on head corrected max ' start_time]);
            
%             max_total_score
%             last_total_score
%             save(['max score2_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'max_score');
% 
%             save(['new score2_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'last_score');
% 
%         
%             save(['min score2_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'min_score');
% 
%             save(['sigma2strange3_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'sigma2') 
        
%             save(['ntol_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'ntol') 
        
            save(['ntols2ndrnd_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'ntols') 
        
            save(['sigmas2ndrnd_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'sigmas') 
        
            save(['scores2ndrnd_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'scores') 
        
            save(['scoresfwd_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'all_scores_fwd') 

%             save(['last total score2_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'last_total_score');
%         
%             save(['correspondence vector_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
%             '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'c');        
            
%         end
        
            cd([this_moving_dataset_directory '/Run ' num2str(this_moving_run_number) '/CrossTransforms']);
            save(['complete cross transform corrected_' moving_filename ' al Btc_Ds' num2str(this_target_dataset) '_Pos' num2str(this_target_position) '_Co0_St' num2str(this_target_stack_number)...
            '_r' num2str(this_moving_run_number) '. ' start_time '.mat'],'complete_cross_transform_corrected')
        
    end
end