fixing code

cleaning up code and checking some math.