Fixed potential memory leaks galore