collapse() now passing all tests. It uses flatten() for level=1 cases and, if flatten raises a TypeError, switches to using the slower but more flexible  _collapse()