Added if's to protect the code against undefined data or templates