Fix memory leak
Fix another leak and simplify code