 (defun company-ycmd-get-annotation (candidate)
   (format " [%s]" (get-text-property 0 'kind candidate)))
 
+(defun company-ycmd-grab (regexp &optional expression limit)
+  (when (looking-back regexp limit t)
+    (or (match-string-no-properties (or expression 0)) "")))
+
+(defun company-ycmd-get-prefix ()
+  (and (memq major-mode company-ycmd-modes)
+       buffer-file-name
+       (ycmd-running?)
+       (not (company-in-string-or-comment))
+       
+       (let* ((candidates (company-ycmd-candidates))
+              (prefix (company-grab-symbol-cons "\\.\\|->\\|::" 2)))
+         (setq company-ycmd-last-candidates candidates)
+         (if candidates prefix))))
+
+(defun company-ycmd-get-candidates (prefix)
+  company-ycmd-last-candidates)
+
 (defun company-ycmd-backend (command &optional arg &rest ignored)
   (interactive (list 'interactive))
 
   ; See company.el for more info.
   (case command
     (interactive (company-begin-backend 'company-ycmd-backend))
-    (prefix (and (memq major-mode company-ycmd-modes)
-                 buffer-file-name
-                 (ycmd-running?)
-                 (not (company-in-string-or-comment))
-                 (company-grab-symbol)))
-    (candidates
-     (cons :async
-           (lambda (cb)
-             (funcall cb (company-ycmd-candidates)))))
+    (prefix (company-ycmd-get-prefix))
+    (candidates (company-ycmd-get-candidates arg))
     (meta (company-ycmd-get-metadata arg))
     (annotation (company-ycmd-get-annotation arg))
     (no-cache 't))) ; Don't cache. It interferes with fuzzy matching.

(defcustom company-ycmd-modes '(c++-mode python-mode csharp-mode)
  "The list of modes for which company-ycmd will attempt completions.")

(defun company-ycmd-construct-candidate (src)
  (let ((candidate (assoc-default 'insertion_text src)))
    (dolist (prop company-ycmd-completion-properties candidate)

  (if (ycmd-running?)
      (mapcar
       'company-ycmd-construct-candidate
       (assoc-default
        'completions
        (with-local-quit
          (ycmd-get-completions (point)))))
    nil))

(defun company-ycmd-get-metadata (candidate)
