fixed some potential leaks/bugs