fixed errors in calculate function