Some refactoring of code for setting template and layout