Modified the loop to avoid a garbage value.