Clean up and fixing possible memory leaks.