tpl refactoring and bugfixes
tpl refactoring and bugfixes