refactoring to state pattern