A recursive solution works like a charm.