replaced anonymous function with foreach to access 'self'