Improved get selected names function.