successfully refactored `insert_sorted' into a stand-alone function