changed range to xrange for efficiency