Changed the code and comments to make more mathematical sense.