I suggest you try to leverage commander's built-in logic for this as much as you can.