more refactoring; db code added