Modified do loops to avoid a check in each loop