Bug fixes in state.c