Refactor special error handling to be less ugly