add tests & fix memory leak