added some foreach code for fast computing