implementing binary search where necessary