Fixed the DB and made some refactoring