refactor db access.