Replaced range with xrange

Performance updates in looping as recommended by
https://www.youtube.com/watch?v=OSGv2VnC0go
Note that some other optimizations could not be performed due to them
being available only in Python3.