made code much more memory efficient for large runs
(#563)