Added comments to explain very obscure code

Added comments to the math functions in the beginning to help explain the reasons for each one and be able to understand what is being done quicker.