refactored onSelectionChange to make it more readable