More refactoring; modularized db a bit