EU agrees to resettle 40,000 refugees