Clean up + fixed a few memleaks.