Fix memory leak and remove debugging messages