This is a work in progress. Feedback is welcome, but I need to clean-up before applying to master.
Okay, so I am currently happy with this first proposal. I will probably try to improve it latter, but in the meantime I think it can be applied to master.
Any feedback on the code?