Refactor and add functionalities to database.