tried binary search; has bugs