Made tree structure and several helper functions.