Correct error in logic of replace_child_by_id routine and add contracts.