Simplify the code to find an available file name to write