2nd draft. Not yet tested but should be more or less complete.