I think this is nearly working.