Refactor templating mechanism