Updated loop, from for to foreach with additional variable