implemented basic recursive algorithm.