fixed minor issues in inner loop