fixed memleaks and etc.