update global state structure. fix some bugs.