Added templates and refactored