make sure that selected is defined before removing mx elements as well as sorting on the inital set of available items to get the index right.