fixed small bugs and memmory leaks