initial changes for issue #2196

You can remove the if, but using for j in xrange(ds1, ...). ... is the max number of iteration. It is excluded.