Redirect to login and some refactoring