Setting Todomap mapping from patch is buggy

i’ve noticed 2 quite interesting bugs:
the first is if i select a variable through the todomap node, hide the todomap window, hit select variable, toggle learn mode and move a controller from an enabled device, it throws an exception saying “Object reference not set to an instance of an Object.” if i move the controller while the mainloop is frozen it’ll register all midi messages as separate inputs (so the same controller is listed 20-50 times in the Inputs section) after clicking continue the mapping works only this disturbs me a bit.
2nd is connected a little bit if i select a variable INSIDE Todomap then select an other variable through the patch then do the same learning method it throws an exception saying “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.”. Same symptom applies here than the previus bug but there is a bigger problem both of the variables (the one selected inside todomap window before the one selected in the patch) gets that input mapping and both reacts to the assigned controller (however the situation suggests that only the last selected variable (the one selected from the patch) should react to that controller).
it would be nice to fix this because this way i could set mappings in md.vis by just right clicking on a slider and moving a controller so i don’t have to search and set it inside todomap window.

example patch and step-by-step reproduce (20.9 kB)