fixed each loop bugs.