Move credential handling code to separate module