The basis of this plugin was imported from a function I wrote previously in non plugin format.