Refactor error handling a bit.