public IAction <TActor>?NextAction() { if (StoredAction != null) { // Return StoredAction and simultaneously set it to null var temp = StoredAction; StoredAction = null; return(temp); } if (Routine != null) { IAction <TActor>?action = Routine.NextAction(); if (action != null) { return(action); } else { Routine = null; } } IActivity <TActor>?activity = NextActivity(); if (activity == null) { return(null); } if (activity is IAction <TActor> atomicAction) { return(atomicAction); } else { Routine = activity.AsRoutine(); return(NextAction()); } }