cuzic's answer with recursive call