have a look at this thread.
there are several other similar ones on the forum.
Here’s another that was based on my original code.