bug fixed with recursive function