fixed some leaks and small bugs