first version of the recursive function with bugs