Fixed bug with recursive function