switched loop order for speed