Rewrite

Refinements to template structure and naming. Added static detection of
reference cycles! Cleaned and improved unittests.