Add memory scalability. Some code cleanup.