Implement search state sync. Refactor a bit to improve code reuse.