Refactoring and tests for templates.