Fix many resource_tpl bugs, clean useless code