pretty much working, except for small memory leak