adding some comments and doing a minor cleanup to make it easier for Jim Blandy to follow the code