fixed foreach and if execution