fixing memory leak and double free