improve recursive algorithm, update documentation