refactored with foreach