Added more checks for division by zero.