Some refactoring to authentication and contexts