refactored BST code