Tidy up error handling a bit