Fix bugs using templates with states and add simple states test.