refactored code, added some tree stuff