template building - improved