Fixed bugs in calculation