Refactor state updating logic.