Refactor Tree and improve API