I think this should work, but it will need testing...