Add state pattern.
TODO: Many bugs need to be fixed.