Added in automatic DB switching logic