Move login code to a separate function from main()