second version of pruning (much more effficient) + bug corrections