Refactors DB connection