Add memory scalability. Some code cleanup.
Bugfixes.