Fixed mismatched free/delete; removed one dangerous and unused function; added missing memory release calls