What `cleanup` is doing should either be moved within the `removeStage` method within `removeJobAndIndependentStages`, or that `removeStage` should be go into the outer scope and be combined with `cleanup`.  Personally, I don't much care for a generically named function like `cleanup` at the outer scope.