First implementation, recursion. Improved efficiency.