refactoring with addStates and retainStates