Fixed memory leaks (thanks to zecoxao).