Thanks to the many contributors who updated and improved the patch over the life of this ticket.
Thanks Michael Jung, Charl Botha and Florian Apolloner for review and help on the patch.