Bugs and memory leaks fixes, some functionality extension