updated recursive function