refactoring memory reallocation to take old size argument