split set_selected out into a function