Major model and db refactoring