added retry and wait loop for template availability