bug fixes to memleaks