Improved header writing speed
added xrange