Fixed some memory corruptions/leaks (master)