correctly handle leaf nodes