Add String length check to prevent out of bounds error