Fixed some memory leaks with the help of memgrind