some cleanup and hopefully a reduction in memory leaks