move "if terms[s] is S.NaN" case above