replaced excessive allocations by realloc uses