More memory bug fixes