Factored out duplicate loop; fixed wrong name (child -> predecessor)