Fixed memory leak and free