More refactoring. DB optimization