refactor state management