rewrote template handling