Code Clean up and bug fixes

The data generated by this R code has been spot checked by hand for a
few values at the beginning of the file to verify its correctness.