Realloc only when necessary