Fixed some compile errors, cleaned up some math.