I know that this method is *really* long but I doubt that separating this would improve readibility. The new method would need all the parameters that this method already gets (which is already a lot) plus the collapsed node ids and it would only be called once (from the point where the code is now)