More bug fixes, mostly related to the math.