fix state switch and add some comments