Fixed a segfault and a memory leak (thx cedric)