Ray's Memory Leak Fix

This includes changes from Ray that should fix memory leaks.