Refactor db connection