Enhanced to handle cleaning up unwanted directories.