Use the correct error handling routine
Be more portable

Using the leading - is a GNU extension (but not documented as such), which
 doesn't work on FreeBSD. Use the more portable way to do the same thing.
Clean up in all cases

Since we use 'set -e', there are plenty of cases where the cleanup code isn't called.
Use 'trap' to ensure that it does.