changed range to xrange and fixed end of 1st for