Use collect method in scan_and_add instead of each. Fixed a few variable references.