added tree struct, moved things into seperate functions