Log dbg and fix memory leaks