Refactored autcompletion logic.