Added length validation to avoid insertion errors.