Major database refactor