Refactored db connections and handling