refactor loop into context method to make it easier to call it twice