refactor db connection code