now writes to file when user closes window