Cleaning, optimize foreach code