moved more data from stack to heap.
bug fix in memory clean-up.