There's something I noticed while testing the script, but it seems like the scripts seems to be seeing the statistics as indexes. I don't know if this is the correct approach so i decided to add the next line to the first query.
and dbo.sysindexes.name not like '_WA_Sys_%'
Thanks for it by the way. I've been trying to do this for some time but never got the time for it 🙁