Fix state machine and release resources.