Update with credits to the author of the original function (which I just found out)