Call flush before release to correctly catch errors.

@NormanHill most of theses changes are cosmetic.  commit 11d0536 needs checking to see if I've done the correct stuff in moving the flush activities out of release and into a new flush method.

There are a few commits that deal with error handling.  Likely those are OK as is.