Restructured mainly by implementing initialize/finalize subs.