WIP: optimize tree construction procedure