Moved template compilation to a function.