refactoring models and database system to be more flexible

fix