Added a check for seeing if the current state is a finishing state.
Some other cleaning.