Fixed math bugs