Mostly rewritten.