Refactored code to fix user and host authentication