Refactored binary search tree code