Solved memory leaks and errors