Moved foreach for faster processing