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.
I don't understand the first `nvars` needs to be cast?  For the other casts, can we use `int(i)` instead of `(int)i`?  Or is that wasteful?