More efficient Fortran loop