Changed range to xrange where necsessary