Building on this very helpful answer from Brian Rogers I wrote the this, but it throws an exception, see more below.
I re-wrote the code, because the original versio