binary search: fix bug, optimize