Freed up the empty space in the replacement array and moved the shrinking reallocs to a separate function