Template refactoring (not complete)