Please see updated (simpler) template code.