Make those SQL Table Indexes pretty

So we have a little issue.. well, that is always relative. One of our SQL instances is slow.. and I mean slooooow. It also happens to store our emails and all our archived documents. The primary reason for this is that none of the indexes have been rebuild/reorganise since forever. A wild Fragmented Index appears.. I chose you Adrian Man.. Adrian Man uses awesome l337 SQL skills. It’s SUPER effective. And so, 1 morning later..

/*-*-*-*-*-*-*-*-* ﷽‎ *-*-*-*-*-*-*-*-*-*-*-*/
SET FMTONLY OFF/*This is for temporary tables in SSRS*/

DECLARE @Database VARCHAR(255), @Table VARCHAR(255), @cmd NVARCHAR(3500),@cmd2 NVARCHAR(3500), @fillfactor INT, @pagemin INT, @fragmin INT, @WaitFor VARCHAR(12),@ActivateWait VARCHAR(1), @JustShowMe VARCHAR(1)
SET @WaitFor = '00:00:15' /*hh:mm:ss Average time for a big index is around 2 minutes, and assume around 25% the trans log filled by each rebuild*/
SET @fillfactor = 80 /*Depending or your index growth anywhere between 70-90 should work*/
SET @pagemin = 1000 /*not concerned with small tables, ignore less than 1500 pages index*/
SET @fragmin = 5 /*Ignore fragmentation less than 5%*/
SET @ActivateWait = 0 /*1 will active it*/
SET @JustShowMe = 1 /*0 is off*/
DECLARE @Databases TABLE(id INT IDENTITY(1,1), databasename VARCHAR(250))
INSERT INTO @Databases
SELECT name FROM sys.databases db
WHERE name NOT IN ('master','msdb','tempdb','model','distribution'
,'MUR Group Views'
,'SAP_test'
,'SAPERION-1'
,'SAP-JHB')
AND db.state <> 6
AND db.user_access <> 1
ORDER BY 1
--SELECT * FROM @Databases
IF @JustShowMe = 0
BEGIN
 DECLARE @Outputme TABLE (id INT, type_desc VARCHAR(50), database_id INT, object_id VARCHAR(250),tablename VARCHAR(250),index_id INT,name VARCHAR(250),page_count INT, avg_fragmentation_in_percent DECIMAL(10,3), seconds INT)
END
DECLARE @i_Count INT, @i_Max INT
SET @i_Max = (SELECT MAX(id) FROM @Databases )
SET @i_Count = 1
WHILE @i_Count <= @i_Max
BEGIN
SET @Database = (SELECT databasename FROM @Databases WHERE id = @i_Count)
SET @cmd2 = 'USE ['+@Database+']
 DECLARE @IndexBase TABLE(id INT IDENTITY(1,1), type_desc VARCHAR(50), database_id INT, [object_id] INT, [tablename] VARCHAR(250), index_id INT, name VARCHAR(250), page_count INT, avg_fragmentation_in_percent DECIMAL(10,3), seconds INT)
 INSERT INTO @IndexBase
 SELECT
 b.type_desc
 , a.database_id
 , b.[object_id],''[''+ isc.TABLE_SCHEMA +''].[''+ t.name+'']''
 , a.index_id
 , b.name
 , a.page_count
 , a.avg_fragmentation_in_percent
 , CASE
 WHEN a.page_count < 3000 THEN FLOOR((a.page_count/100)*0.3)
 WHEN a.page_count > 3000 AND a.page_count < 30000 THEN FLOOR((a.page_count/100)*0.4)
 WHEN a.page_count > 30000 AND a.page_count < 300000 THEN FLOOR((a.page_count/100)*0.5)
 WHEN a.page_count > 300000 THEN FLOOR((a.page_count/100)*0.6)
 END Seconds
 FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID(N''Production.Product''),NULL, NULL, NULL) AS a
 JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id
 LEFT OUTER JOIN sys.tables AS t ON b.[object_id] = t.[object_id]
 LEFT OUTER JOIN INFORMATION_SCHEMA.TABLES isc ON isc.TABLE_NAME = t.name
 WHERE page_count > '+CONVERT(VARCHAR(10),@pagemin)+'
 AND a.avg_fragmentation_in_percent > '+CONVERT(VARCHAR(10),@fragmin)+'
 ORDER BY b.type_desc ASC, page_count desc

 IF EXISTS(SELECT * FROM @IndexBase ib WHERE ib.type_desc = ''CLUSTERED'')
 BEGIN
 IF ' + @JustShowMe + ' = 1
 PRINT 1
 ELSE
 DELETE FROM @IndexBase
 WHERE type_desc <> ''CLUSTERED''
 AND tablename IN (SELECT DISTINCT tablename FROM @IndexBase ib WHERE ib.type_desc = ''CLUSTERED'')
 END
 '

SET @cmd = @cmd2 + '
 DECLARE @Numbers TABLE(number INT);DECLARE @Index AS INT, @Until INT, @FinalCMD NVARCHAR(3800)
 SET @Index = 1
 DECLARE @ibname VARCHAR(250), @ibtablename VARCHAR(250), @waittime INT, @frags INT
 SET @Until = (SELECT MAX(id) FROM @IndexBase)
 WHILE @Index < @Until + 1
 BEGIN
 SET @frags = (SELECT ib.avg_fragmentation_in_percent FROM @IndexBase ib WHERE ib.name IS NOT NULL AND ib.id = @Index)
 SET @waittime = (SELECT ib.seconds FROM @IndexBase ib WHERE ib.name IS NOT NULL AND ib.id = @Index)
 SET @ibname = (SELECT