--- a/src/json_generator.c
+++ b/src/json_generator.c
@@ -176,6 +176,18 @@
 	assert(options != NULL);
 	assert(data != NULL);
 
+#define MJSON_GENERATOR_ALLOC_STRING if( (text = malloc(sizeof(json_str_t))) == NULL) \
+						{ \
+							data->error_code = JSON_ERROR_MEMORY; \
+							return JSON_TOKEN_ERROR; \
+						} \
+						if( (text->text = malloc(options->max_string_length*sizeof(char)) ) == NULL) \
+						{ \
+							data->error_code = JSON_ERROR_MEMORY; \
+							return JSON_TOKEN_ERROR; \
+						} \
+						text->length = 0; /* points to the '\0' */ \ 
+						data->error_code = JSON_ERROR_OK;	/* just in case this had been set */ 
 
 	while(1)
 	{
@@ -235,35 +247,12 @@
 						break;
 
 					case '"':
-						if( (text = malloc(sizeof(json_str_t))) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						if( (text->text = malloc(options->max_string_length*sizeof(char)) ) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						text->length = 0;	/* points to the '\0' */
-						data->error_code = JSON_ERROR_OK;	/* just in case this had been set */
+						MJSON_GENERATOR_ALLOC_STRING;
 						state = 11;	/* a string */
 						break;
 
 					case '0':	/* number that is either 0, frac or exp */
-						if( (text = malloc(sizeof(json_str_t))) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						/*TODO needs to set string limit */
-						if( (text->text = malloc(options->max_string_length*sizeof(char)) ) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						text->length = 0;	/* points to the '\0' */
-						data->error_code = JSON_ERROR_OK;	/* just in case this had been set */
+						MJSON_GENERATOR_ALLOC_STRING;
 
 						text->text[text->length++] = c;
 						state = 25;	/* either 0, frac or exp */
@@ -271,19 +260,7 @@
 
 					case '-':
 						/* this is a negative number */
-						if( (text = malloc(sizeof(json_str_t))) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						/*TODO needs to set string limit */
-						if( (text->text = malloc(options->max_string_length*sizeof(char)) ) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						text->length = 0;	/* points to the '\0' */
-						data->error_code = JSON_ERROR_OK;	/* just in case this had been set */
+						MJSON_GENERATOR_ALLOC_STRING;
 
 						text->text[text->length++] = c;
 						state = 18;	/* negative start part */
@@ -299,19 +276,7 @@
 					case '8':
 					case '9':
 						/* this is an integer part */
-						if( (text = malloc(sizeof(json_str_t))) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						/*TODO needs to set string limit */
-						if( (text->text = malloc(options->max_string_length*sizeof(char)) ) == NULL)
-						{
-							data->error_code = JSON_ERROR_MEMORY;
-							return JSON_TOKEN_ERROR;
-						}
-						text->length = 0;	/* points to the '\0' */
-						data->error_code = JSON_ERROR_OK;	/* just in case this had been set */
+						MJSON_GENERATOR_ALLOC_STRING;
 
 						text->text[text->length++] = c;
 						state = 17;	/* parsing integer */
@@ -964,6 +929,7 @@
 		return JSON_TOKEN_NUMBER;
 	}
 
+#undef MJSON_GENERATOR_ALLOC_STRING 
 }