refactoring code to db