Implemented a version with recursive data structure.