Fix memory leak and get more secure