Improve handling of invalid filenames