Fixed template errors
Refactored code - DRY!!!