Fix loop boundary bug.