Please excuse all the comments. The program produces the right result, I believe.