Refactor and add database model