Added a check for undefined before inserting the 'scripts'