further refactoring and template changes