tweaked to allow nested anchors and to track ids against a collection of targets