Code cleaning and template refactoring.