Added interfaces for API and SSO to allow mocking. Improved error handling.