Binary search implemented to speed things up.