updated to working and converted all foreach to line