Make selectionchange handling more robust

Handling the selectionchange event caused no known errors,
but it seems safer to do it this way.