refactor db access