Refactored to use foreach to prevent error with closures. More work needed!