After some thinking and some work, enjoy: