A little cleanup.

Shorten long lines, extract a function, and add comments.
Merge pull request #1540 from braddunbar/template

A little cleanup.