tidy binarysearch algorithm