fix memory leak; add reference count to object;
refactor code; make lambda more formal;