moving tree code to directive