Fixed a couple of uninitialized variables.
Fixed bad allocation
Fix some memory leaks.