Move code to set credentials to its own function