solved using recursion