Memory allocation and deallocation fixed, more useless code removed.