Unintended Change of Data Folder

I'm not an Igor newbie but I'm noticing some behavior with my data folders that I don't understand. My current data folder structure is as follows:

Root
Root:Elm -->Contains global variables for panel
Root:Elm:'10000': -->Contains data for dataset ID 10000
Root:Elm:'10000':PM -->contains subset data

When I enter this function I am in the data folder PM

Function maskcalc(refPM, elmpm)
    wave refpm, elmpm
    string elmpmname = nameofwave(elmpm)
    NVAR maxPM = root:elm:g_maxPM
    NVAR maxelm = root:elm:g_maxElm

   
    if(stringmatch(elmpmname, "*temp*"))            //if doing a temperature calibration
        duplicate/O refpm, mask_temp
        mask_temp = (refpm[p] > maxelm ) ? 0 :1
    else
        duplicate/O refpm, maskwave
        maskwave = (refpm[p] > maxPM || elmpm[p] > maxelm ) ? 0 :1    //Current data folder gets moved to Root:Elm
    endif

end


Why does the current data folder get moved to Root:Elm. I didn't set a new data folder?

Thanks
Some for info for those who may be able to help:

When I set a breakpoint to catch the debugger, an error gets printed to the command window:
** CurveFit gave error: Mask wave was null or missing

However when i undo the breakpoint and don't catch the debugger, the CurveFit Function works just fine
fit_ref_dusttrak_dustPM2_5= poly(W_coef,x)
W_coef={-0.02849,2.6754e-005,1.5411e-008}
V_chisq= 23870.2;V_npnts= 45684;V_numNaNs= 26624;V_numINFs= 0;
V_startRow= 0;V_endRow= 72322;
W_sigma={0.0161,1.02e-05,6.58e-09}

I've attached some images