breaking out tree into separate files/functions

Breaking out tree into separate functions, original file is still in place and has been cleaned up. Original functionality still in place.