Refactored credentials handling to use database