new version based on templates instead of macros