fix id selection issue

Fixes #1041 fixes #1073 fixes #1074

Hat tip to @cohitre for finding this elegant solution!