Add new database logic, fix rewind bug