Major refactor in preparation for search.