Took me a bit of time to write, but here it is.