Flush the clipboard on exit

Apparently flushing immediately after setting the clipboard sometimes
doesn't work.
Eliminate some duplicated exception handling code