refactoring with states