Some small refactoring for autocomplete

Still a long way to go.