Refactoring for db layer