fixed possible issue in foreach