ENH: Made directory handling more robust