Refactor error handling slightly