refactored loading credentials code