refined the code with each.do