Improve temporary filename handling of special cases