Now using mkstemp instead of tmpnam for thread safety.