binary searching. Possibly buggy. need to check the stop conditions.