Fix math bugs