IMPROVE: add trap and set -e

  This will ensure errors are not ignored and cleanup function
  is invoked if anything bad happens.