modified getFirstValidOption function