Completed findBestShift function