Fixed potential for writing last file twice