Initial cut, seems to work, mostly. Needs a lot more testing.