Fix the bad part of the code which caused too much memory consumption