Added a condition to avoid overflow