Cleanup autocomplete code and fix tests