Improved binary tree version