[wip] core: Add tests, fix memory leak.