Refactor the state pattern