Changed many xrange to range for Py3 compatibility.