Removing extra foreach loop