STYLE: while loops merged into a single for loop for clarity