With pruning and updated sorting logic