fixed realloc bug, cleanup