Clean up template sync code, and update template state in template sync
phase instead of doing it in adding image store.