Fixed memory leak and overrun