added code to account for invalid selected-index values