Statistical errors resolved and comments added.

I believe that all errors have been resolved and have improved commenting to the code in places.