Fixed random error when reallocating memory