Bugs + memory leaks  fixes