Tidy up error handling a little