Fix and refactor templates