added most of the math that is needed