function [events,event_fits]=phasic_and_asynchronous_event_extractor_core(Imem_thistrial,AP_times_thistrial,trial,return_all_postAP_windows,template,template_seeds,event_thresh,...
    event_jitter,event_aligned_extraction,event_window,baseline_offset_window1,baseline_offset_window2,fit_slope_thresh,plot_sloperemovedextractions,plot_extracted_events,...
    fit_events,fit_latency_window,return_pareddown_fitinfo,plot_fits,plot_fit_residuals)
%description: created to provide the option of running in parallel for loops for speed or conventional for loops for plotting
    %receives data and detection template
    %does template-match detection, finds latencies and other timing information, then extracts waveforms. after subtracting decay current if the event is riding on any,...
        %the event can be fit. peak amplitude and charge can be calculated on the fit. 
    %the APtimes that are passed in should reflect the earliest detected action (sodium) current
    %return_all_postAP_windows essentially overrides detection and pretends an event happened beginning at the eponymous number of samples following every AP
    %event_aligned_extraction set to zero will return membrane current beginning at the AP
    %event_window is typically -6:57 -- in samples, the membrane current information that will be returned for each event, with time 0 being the time of the peak of the detection criterion
        %if an onset taper is used, may want to extend event_window accordingly
    %baseline_offset_window1 is then 1:5 -- the indices of the samples that will be averaged and subtracted from the event for baseline removal if slope > fit_slope_thresh
    %and baseline_offset_window2 is often 47:57; 
    %fit_slope_thresh determines how much underlying slope has to be detected before the attempt is made to fit it and remove it
%called by: phasic_and_asynchronous_event_extractor
%calls: template_based_mini_detector, find_threshold_cross_peak_vals_and_times, event_time_rel_AP_times, event_extractor_core, iterative_curve_fit_singledecay
%notes: each returned event is prefixed with seven values, in this order: trial, which AP the event followed, latency, time of occurrence rel to first AP (this value...
    %is for cum hist of occurrence times), magnitude of the criterion peak, the absolute time of onset (can also be used for cumulative histograms), and whether the event was slope-corrected
%code confirmed date: feb 4, 2013

%perform template-match detection
dcz=template_based_mini_detector(Imem_thistrial',template);

if return_all_postAP_windows
    crit_peaks=AP_times_thistrial+return_all_postAP_windows;
    crit_peaks=[crit_peaks;nan(1,length(crit_peaks))]
else %find peaks
    crit_peaks=find_threshold_cross_peak_vals_and_times(dcz,event_thresh,event_jitter)
end

if ~isempty(crit_peaks)
    [which_APs,rel_event_times,time_rel_first_AP]=event_times_rel_AP_times(crit_peaks(1,:),AP_times_thistrial);

    %extract events
    events=event_extractor_core(Imem_thistrial,event_aligned_extraction,which_APs,AP_times_thistrial,crit_peaks(1,:),rel_event_times,event_window,baseline_offset_window1,...
        baseline_offset_window2,fit_slope_thresh,plot_sloperemovedextractions,plot_extracted_events);

    %prepend each extracted event with a measure of its delay relative to the closest preceding AP (if no preceding AP, this is negative number) & other information
    %create_basic_text_file_for_asynch_rel_INa_onset has provided the first detectable sample with action current as the AP time, so latency is relative to this
    events=[repmat(trial,1,size(crit_peaks,2));which_APs;rel_event_times;time_rel_first_AP;crit_peaks(2,:);events];

    event_fits=[];
    if fit_events
        for j=1:size(events,2)
            event_fit=iterative_curve_fit_singledecay(events(:,j)',[template_seeds 0 0],fit_latency_window,return_pareddown_fitinfo,plot_fits,plot_fit_residuals);
            event_fits=[event_fits event_fit'];
        end
    end
else
    events=[];event_fits=[];
end