Version 2.0 using references instead of recursion for organizing the tree like structure