The following needs more comments. It is somewhat convoluted.