reorganizing formula logic