clean up code, fix memory leaks