WIP, and fix memory leaks