Improve error handling code

This fixes one of the warts from the last commit that I was unhappy
about.