Move the whole 'update current state' logic into set_state. Also make insert_object commit database changes