more leak fixes and const ref correctness