Function now handles invalid string