Fix for can't write file target is directory