Refactor and simplify some of the credential extraction code.