fixed some bugs and leaks