tpl code refactored