Some changes to file management and exchanged range() for xrange().