refactored sml code for bst