bunch of math I took from Wikipedia, initially it didn't work but I changed some stuff then it worked??