Remove state_end.c, fix some bugs