changed to use recursion