Refactoring and enhance authentication mechanism