Performance improvements and memory leaks