Major refactoring of database