regactor and add templating