refactoring of internal tree functionality