Changed to use xrange, uses less memory