Found few memory errors; fixing leaks