Major refactoring to take into use the new database schema