There was an error in the strcpy in that space was not being allocated for the copy and was therefore being overwritten each time. Used strdup instead which did allocate space properly