Fix bug in memory reallocation