fixed error handling in calculate()