protected State<TransitionInput> exitCurrentState(State<TransitionInput> newState, State<TransitionInput>... submachineStates) {
		if(currentState == null) {
			return null;
		}
		State<TransitionInput> previousState = currentState;

		if(submachine != null) {
			submachine.exitCurrentState(getFirstState(submachineStates), getRemainingStates(submachineStates));
			submachine = null;
		}

		if(!currentState.equals(newState) || submachineStates.length == 0) {
			for(StateMachineEventListener<TransitionInput> listener : eventListeners) {
				listener.beforeStateExited(currentState, this);
			}
	
			currentState.onExit();

			for(StateMachineEventListener<TransitionInput> listener : eventListeners) {
				listener.afterStateExited(currentState, this);
			}
	
			for(CompositeState<TransitionInput> composite : determineCompositeStatesBeingExited(currentState,newState)) {
				exitCompositeState(composite);
			}

			currentState = null;
		}

		return previousState;
	}