Fixed (almost) all memory leaks