Fix some leaks and warnings