Fixed bugs and memory leaks