Added comments to explain some of the madness.