fully recursive algorithm