function [collected, og_input, output_sum, feedstock_out, pile_size, trash_counter, trash_sum, moisture, cum_DML,pile_counter] = FIFO(period,UL,q,og_input,DML,max_time,max_draw,timestep,w_r,property_array,k,collected,max_storage,process_delay)
%First in first out
%This is the unpiling model

% Initialize variables
feedstock_out = zeros(1,q);
pile_size = zeros(1,q);
output_sum = zeros(1,q);
cum_DML = zeros(1,q);
trash_counter = zeros(1,q);
trash_sum = zeros(1,q);
contract_sum = 0;
contract_count = 0;
period_index = 1;
accepted_feedstock = 0;
denied_feedstock = 0;

% Loop through timestep
for ind1 = 1:q
    % ind1 represents the current timestep

    % Set up contract. If upper limit (UL) reached, truckload not accepted
    contract_count = contract_count + 1;
    if contract_sum > UL
        % If UL reached, reject truckload
        og_input(ind1) = 0;
    elseif contract_sum + og_input(ind1) > UL
        % If truckload would overflow upper limit, accept partial truckload
        og_input(ind1) = UL - contract_sum;
        contract_sum = contract_sum + og_input(ind1);
    else
        % Accept full truckload
        contract_sum = contract_sum + og_input(ind1);
    end
    % If end of period reached, reset period & sum
    if contract_count == period(period_index)
        contract_count = 0;
        period_index = period_index + 1;
        contract_sum = 0;
    end

    % Set pile size before DML or output to process. Previous pile size now has input from truck dump
    if ind1 == 1
        if og_input(ind1) > max_storage
            % If truckload would overflow maximum storage, accept partial truckload
            accepted_feedstock = max_storage;
            denied_feedstock = og_input(ind1) - accepted_feedstock;
            og_input(ind1) = accepted_feedstock;
            pile_size_preDML(ind1) = accepted_feedstock;
        else
            % Accept full truckload
            accepted_feedstock = og_input(ind1);
            denied_feedstock = 0;
            pile_size_preDML(ind1) = og_input(ind1);
        end
    else
        if pile_size(ind1-1)>max_storage
            % If pile is greater than maximum storage, reject truckload
            accepted_feedstock = 0;
            denied_feedstock = og_input(ind1) - accepted_feedstock;
            og_input(ind1) = accepted_feedstock;
            pile_size_preDML(ind1) = pile_size(ind1 - 1);
        elseif pile_size(ind1-1) + og_input(ind1) > max_storage
            % If truckload would overflow maximum storage, accept partial truckload
            accepted_feedstock = max_storage - pile_size(ind1-1);
            denied_feedstock = og_input(ind1) - accepted_feedstock;
            og_input(ind1) = accepted_feedstock;
            pile_size_preDML(ind1) = pile_size(ind1-1) + accepted_feedstock;
        else
            % Accept full truckload
            accepted_feedstock = og_input(ind1);
            denied_feedstock = 0;
            pile_size_preDML(ind1) = pile_size(ind1 - 1) + og_input(ind1);
        end
    end

    % Reinitialize for each day
    pile_counter(ind1) = og_input(ind1);
    collected = 0;
    cum_property = 0;
    trash = 0;

    for ind2 = 1:ind1
        % ind2 represents all days leading up to current timestep
        [skip,cum_DML,collected,cum_property,pile_counter,trash] = FindDML(ind1,ind2,pile_counter,DML,cum_DML,max_time,cum_property,max_draw,timestep,w_r,property_array,k,collected,trash,process_delay);

        if skip
            continue; % This is how to "GoToNext" in VBS
        end
    end

    % Find pile size now that dry matter has been calculated
    feedstock_out(ind1) = collected;
    pile_size_postDML(ind1) = pile_size_preDML(ind1) - cum_DML(ind1);
    pile_size(ind1) = pile_size_postDML(ind1) - collected - trash;

    % Calculate feedstock in (amount bought includes denied feedstock
    og_input(ind1) = accepted_feedstock + denied_feedstock;

    % Calculate total output
    if ind1 == 1
        output_sum(ind1) = collected;
    else
        output_sum(ind1) = collected + output_sum(ind1 - 1);
    end

    % Calculate moisture content
    if collected == 0
        moisture(ind1) = 0;
    else
        moisture(ind1) = cum_property / collected; % fraction of water in ith days feedstock
    end

    % Calculate total feedstock discarded (due to age) and denied (due to storage constraints)
    trash_counter(ind1) = trash + denied_feedstock;
    trash_sum(ind1) = sum(trash_counter,"all");

end