solved using generator (recursive)