Factor autocompletion choice finding to a function