Replace unnecessary foreach