Really fixed the script to correctly handle directories