optimize inner loop a bit