finished the templates and added some further improvements

I've completed the templatization so everything compiles again.

I also did some cleanup and code hardening.

You may merge these changes into your branch, update PR #18 on the main repo and merge PR #18 into the release branch.
Can we keep the space after `for`?  (And everywhere else too?)