Created the forEachElement method for handling side effects