Moved the empty check before the foreach loop