reorganizing foreach stuff