Restructure foreach