JM_fun_optimized