Updated to handle error cases and roles.