Basically works

... but it's a hell of a hack. Needs much cleanup.