Learning about state machine by copying jakesgordon.