Fixed issue with conditions in loops