Implementation of the state pattern.