added safeguards to prevent removing or overwriting top-level paths