Fixed memory bug. Discovered another bug