performance improvements, replacing range by xrange