moving the callback out of the foreach