improvement to ranSelect function