little bit of refactoring to make the functions in template.py easier to
understand and use