fixed bug which greatly increased memory needed