add a case to check if the return value is overflowed, and slight optimization