Move searchRoot loop iteration to a separate function