Collapsing nodes work for nested layers

Recursion algorithm will further collapse child nodes, if applicable.