Refactor to extract directory walking to an external command