Refactored db connect