Fixed some memory issues. There are still more.
merged conflicts